Skip to content

Latest commit

 

History

History
26 lines (17 loc) · 809 Bytes

InferArrayItem.md

File metadata and controls

26 lines (17 loc) · 809 Bytes
标题 标签
InferArrayItem(提取数组中的元素类型) infer,array(推断,数组)

提取数组中的元素类型。

  • 使用 infer 关键字推断数组T的元素类型,如果T继承数组R类型,则返回R,否则返回T。

代码如下:

type InferArrayItem<T> = T extends (infer R) [] ? R : T;

使用方式:

type InferString = InferArrayItem<string []>; // string;
type InferStringOrNumber = InferArrayItem<string | number []>; // string;

应用场景

如下所示,鼠标悬浮到对应的类型变量可以查看类型。