gpt4 book ai didi

typescript - 当启用标志 --strictNullChecks 时,null 不可分配给 void

转载 作者:搜寻专家 更新时间:2023-10-30 21:45:19 24 4
gpt4 key购买 nike

根据 TypeScript 文档:

However, when using the --strictNullChecks flag, null and undefined are only assignable to void and their respective types. This helps avoid many common errors. In cases where you want to pass in either a string or null or undefined, you can use the union type string | null | undefined. Once again, more on union types later on.

但是,当我尝试以下代码片段时:

// This worked correctly
let voidIsUndefined: void = undefined;

// While following code thrown a compilation error "Type 'null' is not assignable to type 'void'."
let voidIsNull: void = null;

它没有完全按预期工作。

根据文档,我应该能够将 null 分配给 void 类型。有人可以消除这个疑问吗?

最佳答案

TypeScript docs具有误导性,您感到困惑是正确的。这里有一个问题:https://github.com/microsoft/TypeScript-Handbook/issues/468

关于typescript - 当启用标志 --strictNullChecks 时,null 不可分配给 void,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56408929/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com