TypeScript Extract

Extract<T, U>工具类型与Exclude<T, U>工具类型是互补的,它能够从类型T中获取所有可以赋值给类型U的类型。示例如下:

function add(): void {
   console.log("coolcou.com");
}

type T0 = Extract<'a' | 'b' | 'c', 'a' | 'f'>; // 'a'
type T1 = Extract<string | (() => void), Function>; // () => void
type T2 = Extract<string | number, boolean>;        // never

const x: T0 = "a";
const y: T1 = add;

console.log(x)
console.log(y)

运行结果:

TypeScript Extract

酷客网相关文章:

赞(0)

评论 抢沙发

评论前必须登录!