Wayou

unknown 字面理解和 any 其实没差,任何类型都可赋值给它,但有一点,

Anything is assignable to unknown, but unknown isn’t assignable to anything but itself and any without a type assertion or a control flow based narrowing

--TypeScript 3.0 Release notes - New unknown top type

unknown 类型不能赋值给除了 unknownany 的其他任何类型,使用前必需显式进行指定类型,或是在有条件判断情况下能够隐式地进行类型推断的情况。

下面代码是合法的:

let a: unknown;
const b: unknown = a;
const c: any = a;

因为 unknown 是可以赋值给 unknown 的,而下面的代码则不行,

let a: unknown;
//             
                

分类:

技术点:

相关文章:

粤ICP备12038626号 Powered By WordPress . Theme by Luju