Results 3 comments of Alison

let num: number = undefined; // 严格模式下,会报错

声明变量的数据类型为 void 时,非严格模式下,变量的值可以为 undefined 或 null。而严格模式下,变量的值只能为 undefined。

> `let num: number = undefined;` > 这个确实不会报错啊,可能是因为你们用编译器是有做校验吧,我用cmd写是没报错的 这个不是编译器有校验,是在 tsconfig.json 配置中。strict 属性值设置为了 true,也就是采用 严格模式,严格模式下,会报错: Type 'undefined' is not assignable to type 'number'。 这其实和你通过cmd 运行还是浏览器查看,没有多大的关系。