![](/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.