简体   繁体   English

错误 TS2532:对象可能是“未定义” - 使用“?” 操作员

[英]error TS2532: Object is possibly 'undefined' - Using "?." operator

I use TS 4.0.2, and get an error while using "?."我使用 TS 4.0.2,并在使用“?”时出错。 operator:操作员:

在此处输入图片说明

When I use the old nullish check, it run:当我使用旧的无效检查时,它运行:

if (a && a.length) { 

What do I miss?我想念什么?

If you read the documentation |如果您阅读文档 | 3.7 you will see that. 3.7你会看到的。

That might result in dividing undefined, which is why in strictNullChecks, the following is an error.这可能会导致除 undefined,这就是为什么在 strictNullChecks 中,以下是错误的。

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

I believe that this is the case.我相信情况确实如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对象可能是“未定义” ts2532 - object is possibly "undefined" ts2532 Ts2532、Object 可能是“未定义” - Ts2532, Object is possibly 'undefined' 打字稿数组语义错误 TS2532:对象可能是“未定义” - Typescript array semantic error TS2532: Object is possibly 'undefined' TypeScript 错误 TS2532:Object 可能是“未定义”? - TypeScript Error TS2532: Object is possibly 'undefined'? TS2532:对象可能是“未定义”。 在 array.map() 上 - TS2532: Object is possibly 'undefined'. on array.map() TypeScript 2:Import语句生成TS2532:“对象可能是'undefined'。” - TypeScript 2: Import statement generates TS2532: “Object is possibly 'undefined'.” Typescript 显示“this”错误 Object 说 TS2532: Object 在 vue 方法中可能是“未定义” - Typescript displays error for "this" Object saying TS2532: Object is possibly 'undefined' inside of vue methods 类型保护错误TS2532后,可能未定义Typescript对象 - Typescript Object is possibly undefined after type guard error TS2532 在另一个函数中使用时,变量可能未定义(打字稿“错误TS2532”) - Variable possibly undefined when used inside another function (Typescript “error TS2532) 打字稿-内联未定义检查不起作用(对象可能是'undefined'.ts(2532)) - Typescript - Inline undefined check not working (Object is possibly 'undefined'.ts(2532))
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM