简体   繁体   English

Object 可能是可选链接上的“未定义”错误 Typescript

[英]Object is possibly 'undefined' Error on Optional Chaining Typescript

I have a response which I am accessing: data?.currentOrganization?.onboardingSteps?我有一个正在访问的响应: data?.currentOrganization?.onboardingSteps? . . As you can guess, data, currentOrganization, and onboardingSteps might all be null.如您所料,data、currentOrganization 和 onboardingSteps 可能都是 null。 I want to assign a variable as follows:我想分配一个变量如下:

const hasSteps = data?.currentOrganization?.onboardingSteps?.length > 0;

I thought hasValue would evaluate to false if any of the fields were null or if there was less than 1 step.如果任何字段为 null 或少于 1 步,我认为 hasValue 将评估为 false。 However, I get the TypeScript error: Object is possibly 'undefined' .但是,我收到 TypeScript 错误: Object is possibly 'undefined'

This is how I am currently getting around it:这就是我目前解决它的方式:

const hasSteps =
  data?.currentOrganization?.onboardingSteps != null &&
  data?.currentOrganization?.onboardingSteps?.length > 0;

This feels unnecessarily verbose.这感觉不必要地冗长。 Is there an alternative, more elegant solution?有没有其他更优雅的解决方案?

The optional chain will end up producing a value for data?.currentOrganization?.onboardingSteps?.length which is presumably a number if everything in the chain is not null or undefined .... but if anything in the chain is nullish, then the output will be undefined itself. 可选链最终将产生一个data?.currentOrganization?.onboardingSteps?.lengthnumber ,如果链中的所有内容都不是nullundefined .... 但如果链中的任何内容为空,则该值可能是一个数字output 本身将是undefined的。 You can't test undefined > 0 without Typescript complaining about it.如果没有 Typescript 抱怨,您将无法测试undefined > 0

So you should probably do something like the following:因此,您可能应该执行以下操作:

const hasSteps = (data?.currentOrganization?.onboardingSteps?.length ?? 0) > 0;

This is using nullish coalescing to produce 0 if the optional chain ends in undefined .如果可选链以undefined结尾,则使用无效合并来产生0

Playground link to code Playground 代码链接

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

相关问题 为什么 TypeScript 在可选链接运算符后显示“无法调用 object 这可能是'未定义'.ts(2722)”错误? - Why is TypeScript showing “Cannot invoke an object which is possibly 'undefined'.ts(2722)” error after optional chaining operator? 反应 typescript state Object 可能是“未定义”错误 - react typescript state Object is possibly 'undefined' error 打字稿:对象可能是“未定义” - Typescript: Object is possibly 'undefined' Object 可能在 TypeScript 中未定义 - Object is possibly undefined in TypeScript 对象可能是“未定义”打字稿 - Object is possibly 'undefined' typescript “对象可能是'未定义的'”在reactjs中使用typescript - “Object is possibly 'undefined'” in reactjs with typescript 如何使用 react 和 typescript 修复错误 object 可能未定义? - How to fix the error object could be possibly undefined using react and typescript? 打字稿数组语义错误 TS2532:对象可能是“未定义” - Typescript array semantic error TS2532: Object is possibly 'undefined' TypeScript 错误 TS2532:Object 可能是“未定义”? - TypeScript Error TS2532: Object is possibly 'undefined'? 为什么 typescript 会在一个数字上抛出错误 | 未定义的比较,即使在使用可选链接之后? - Why does typescript throw an error on an number | undefined comparison, even after using optional chaining?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM