简体   繁体   English

为什么 Typescript 允许在 function 中为空/未定义

[英]Why Typescript allows null/undefined in function

Why Typescript allows to pass null / undefined there?为什么 Typescript 允许通过null / undefined那里?

// "strictNullChecks": false
function someFun(param: (foo: any) => any) {}
someFun(null); // no error - incorrect
someFun(undefined); // no error - incorrect

Quoting from the docs文档中引用

In strict null checking mode, the null and undefined values are not in the domain of every type and are only assignable to themselves and any (the one exception being that undefined is also assignable to void)在严格的 null 检查模式下,null 和 undefined 值不在每种类型的域中,并且只能分配给它们自己和任何(一个例外是 undefined 也可以分配给 void)

..T and T | ..T 和 T | undefined are considered synonymous in regular type checking mode (because undefined is considered a subtype of any T), undefined 在常规类型检查模式中被认为是同义词(因为 undefined 被认为是任何 T 的子类型),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM