繁体   English   中英

不允许 null 或未定义的 useState 和 TypeScript?

[英]Dont allow null or undefined with useState and TypeScript?

我将 TypeScript 类型添加到 React 的 useState 挂钩中。

const [checked, setChecked] = React.useState<"checked" | un-checked">(null);

是否可以只允许checkedun-checked值,而不是nullundefined以便上面的代码会出错?

您需要在您的tsconfig.json中启用"strictNullChecks": true

'null' 类型的参数不能分配给 '"checked" 类型的参数 | “未检查” | (() => "已检查" | "未检查")'。

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html

暂无
暂无

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

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