【发布时间】:2018-09-27 08:04:31
【问题描述】:
type Field = {test: {more: number} | null}
let fields: Field[] = [{test: {more: 55}}]
无论类型保护如何,Transpiler 都会抛出错误:
if (fields[0].test) {
fields[0].test.more = 55 // object is possibly null
}
这里没有错误:
function f(field: Field) {
if (field.test) field.test.more = 15 // no error
}
【问题讨论】:
-
我不认为流分析会跟踪数组索引类型......它只对字段和局部变量这样做......
-
@TitianCernicova-Dragomir 这应该是一个答案,它是正确的。
-
@MadaraUchiha 想为此找到参考,但没有时间,以为其他人会得到它。我现在添加了答案并引用了票:)
标签: typescript strictnullchecks