【发布时间】:2021-08-14 10:18:35
【问题描述】:
我有以下代码行:
const allGarments = teeMeasuresAverages || JSON.parse(localStorage.getItem("teeMeasuresAverages")) || teeMeasuresAveragesLocal;
Typescript 抛出此警告:
Argument of type 'string | null' is not assignable to parameter of type 'string'.
Type 'null' is not assignable to type 'string'.
所以我尝试包含非空断言运算符(!):
const allGarments = teeMeasuresAverages || JSON.parse(localStorage.getItem("teeMeasuresAverages")) || teeMeasuresAveragesLocal;
这给了我一个不同的警告:
Forbidden non-null assertion.
我是打字稿的新手。它在这里寻找什么?
【问题讨论】:
标签: reactjs typescript local-storage