![](/img/trans.png)
[英]Typescript function with generic type in useState, allow nullable
[英]Dont allow null or undefined with useState and TypeScript?
我将 TypeScript 类型添加到 React 的 useState 挂钩中。
const [checked, setChecked] = React.useState<"checked" | un-checked">(null);
是否可以只允许checked
和un-checked
值,而不是null
或undefined
以便上面的代码会出错?
您需要在您的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.