【发布时间】:2022-07-10 23:06:51
【问题描述】:
type Person = {
account?: {
money: number
}
}
const person: Person = {}
// case1 - this is not error
if (person.account?.money === 0) {
console.log("I have no money");
}
// case2 - Object is possibly 'undefined'.(2532)
if (person.account?.money > 0) {
console.log("I have money!!");
}
为什么打字稿在案例 1 上不显示错误,而只在案例 2 上显示错误?
有什么区别?
【问题讨论】:
标签: typescript