完全搞不懂,这么标准的代码为什么会报错
static getUserInfo() : UTSJSONObject {
let info : UTSJSONObject = {};
const value:string = uni.getStorageSync(USER_INFO_KEY);
if(value && value!==''){
info=JSON.parse(value);
}
return info ;
}
报错 error: Type mismatch: inferred type is Any? but UTSJSONObject was expected
kmlckj (作者)
确实是这一行
2023-11-22 20:38
kmlckj (作者)
static getUserInfo() : UTSJSONObject {
let info : UTSJSONObject = {};
const value:string = uni.getStorageSync(USER_INFO_KEY);
if(value && value!==''){
info=JSON.parse<UTSJSONObject>(value);
}
return info ;
}
这样也一样报错:error: Type mismatch: inferred type is UTSJSONObject? but UTSJSONObject was expected
2023-11-22 20:39
DCloud_heavensoft
回复 kmlckj: json.parse是可能失败的,会返回null,所以你的info也需要可为空,不然赋不进去
2023-11-22 20:45
kmlckj (作者)
回复 DCloud_heavensoft: 那请教下,该怎么写呢
2023-11-22 20:59
DCloud_heavensoft
回复 kmlckj: 定义info时可为空啊
2023-11-22 21:35