0
我收到了在typescript中使用strictNullChecks模式编译的错误。我可以避免Array的“对象可能'未定义''Typescript错误
interface Hoge {
num?: number;
}
const hoge: Hoge[] = [{ num: 1 }];
if (!!hoge && hoge.length > 0 && !!hoge[0] && !!hoge[0].num) {
hoge[0].num.toString()
}
playground(请strictNullChecks
)
不过,我认为这些条件已经完全避免undefined
类型hoge
了。
我猜非空断言运算符效果不错:hoge[0].num!
,但我想知道是否有其他类型的安全方式。