demo: typescript playground demo
主要代码:
interface Example {
children?: Example[]
}
const example: Example = { children: [{ children: [] }] }
if (example.children) {
for (let i = 0; i < example.children.length; i++) {
if (example.children[i] && example.children[i].children) {
console.log(example.children[i].children.length)
}
}
}
if判断example.children[i].children非空后 下一句依然提示example.children[i].children is possibly undefined
请问为何会出现这种情况 如何能在不使用强制非空断言的情况下解决这个问题?