Skip to content

Files

Latest commit

 

History

History
33 lines (22 loc) · 948 Bytes

Extract.md

File metadata and controls

33 lines (22 loc) · 948 Bytes
标题 标签
Extract(提取类型) extract,extends(提取,继承)

从 T 中提取可分配给 U 的类型。

  • 使用 extends 关键字判断类型 T 是否继承类型 U,是则返回 T,否则返回 never。

代码如下:

type Extract<T, U> = T extends U ? T : never;

使用方式:

type ExtractA = string | boolean | number;
type ExtractB = string;
type ExtractC = Extract<ExtractA, ExtractB>; // string;

type ExtractD = string | number;
type ExtractE = Extract<ExtractD, number>; // number

type ExtractF = Extract<ExtractD, string | number>; // string | number
type ExtractG = Extract<ExtractA, string & number>; // never

应用场景

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