错误TS2532:对象可能是'undefined'-使用“?”。算子

时间:2020-10-19 09:29:30

标签: javascript typescript

我使用TS 4.0.2,并在使用“?”时出现错误。运算符:

enter image description here

当我使用旧的无效支票时,它将运行:

if (a && a.length) { 

我想念什么?

1 个答案:

答案 0 :(得分:4)

如果您阅读documentation | 3.7,将会看到它。

这可能会导致未定义的划分,这就是为什么 strictNullChecks,以下是错误。

function barPercentage(foo?: { bar: number }) {
  return foo?.bar / 100;
  //     ~~~~~~~~
  // Error: Object is possibly undefined.
}

我相信是这样。

相关问题