【发布时间】:2019-12-02 08:42:00
【问题描述】:
试图了解类型之间的关系我有这段代码
type CheckIfExtends<A, B> = A extends B ? true : false;
type T1 = CheckIfExtends<number, unknown>; //true
type T2 = CheckIfExtends<number, {}>; //true
type T3 = CheckIfExtends<number, any>; //true
type T4 = CheckIfExtends<() => void, unknown>; //true
type T5 = CheckIfExtends<() => void, {}>; //true
type T6 = CheckIfExtends<() => void, any>; //true
type T7 = CheckIfExtends<unknown, any>; //true
type T8 = CheckIfExtends<any, unknown>; //true
type T9 = CheckIfExtends<{}, unknown>; //true
type T10 = CheckIfExtends<{}, any>; //true
type T11 = CheckIfExtends<any, {}>; //boolean
type T12 = CheckIfExtends<unknown, {}>; //false
有人能解释一下吗?有什么区别? any extends {} 和 any 怎么可能不会同时扩展 {}?如果any extends unknown 和unknown extends any 那么这是否意味着它们是强相等的?是 JavaScript 的 null 和 undefinded 股权问题之上 Typescript 的新缺陷吗?
其实
type T = CheckIfExtends<any, number>; //boolean
【问题讨论】:
标签: typescript types