发现了 Is<T> 和 Is2 之间一个奇怪的现象:
type Is<T> = (x: any) => x is T;
type IsTrue = Is<true>;
function isTrue(x: any): x is true {
return x === true;
}
let f: IsTrue = isTrue;
type Is2 = <T>(x: any) => x is T;
type FromIs2<T> = ReturnType<(a: Is2) => typeof a<T>>;
type IsTrue_2 = FromIs2<true>;
let f_2: IsTrue_2 = isTrue;
f = f_2; // 编译通过
f_2 = f; // 编译通过
// 虽然上面 f, f_2 可以互相赋值,但是下面的测试却表明 Is2 是 Is 的子类,反过来却不是
type Is_Extends_Is2<T> = Is<T> extends (<T>(x: any) => x is T) ? true : false;
type Is_2_Extends_Is<T> = (<T>(x: any) => x is T) extends Is<T> ? true : false;
type T30 = Is_Extends_Is2<true>; // 得到 false
type T31 = Is_2_Extends_Is<true>; // 得到 true
老师,这种变量之间可以相互赋值,但是类型之间却不等价的现象怎样解释呢?