【发布时间】:2022-01-17 02:04:21
【问题描述】:
在这个片段中我有下一个错误:
Type 'string | null' is not assignable to type 'string'.
Type 'null' is not assignable to type 'string'. TS2322
export async function GetCertainCoinByType(coinId: string) {
const response = await axios.get(URLofCertainCoins + `certain/${coinId}`, {
headers : {
token : localStorage.getItem('token'),
}
});
return response;
}
【问题讨论】:
-
Localestorage getItem 可能会返回 null,因此您需要检查这一点。我个人会抛出异常,因为在没有令牌的情况下调用此 fn 似乎无效。
标签: javascript node.js typescript