Object is possibly 'undefined' Error on Optional Chaining Typescript

Caleb Robinson

I have a response which I am accessing: data?.currentOrganization?.onboardingSteps?. As you can guess, data, currentOrganization, and onboardingSteps might all be 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. However, I get the TypeScript error: 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?

jcalz

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. You can't test undefined > 0 without Typescript complaining about it.

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.

Playground link to code

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Access optional keys in object, where key is a literal in a literal union type without error "Object is possibly 'undefined'"

From Java

Object is possibly 'undefined' when init a variable Typescript

From Dev

Typescript: Object is possibly 'undefined' but only for '<' and '>' operators

From Java

Typescript, how to pass "Object is possibly null" error?

From Java

Typescript optional chaining error: Expression expected.ts(1109)

From Dev

Typescript : Object is possibly null

From Java

How to suppress "error TS2533: Object is possibly 'null' or 'undefined'"?

From Dev

Object is possibly null error Typescript when doing comparison

From Dev

Object is possibly 'undefined' in ternary operation

From Dev

Error: Call to a possibly undefined method

From Dev

Typescript/React: "Object is possibly 'null'"

From Dev

Typescript/React: "Object is possibly 'null'"

From Java

How to use optional chaining with array in Typescript?

From Java

How can I solve the error 'TS2532: Object is possibly 'undefined'?

From Dev

Object is possibly undefined during ternary operator

From Dev

typescript error TS2531 Object is possibly 'null' is NOT being suppressed by strictNullChecks : false

From Dev

Prop is possibly undefined error in Vue 3 setup

From Java

How to enable optional chaining with Create React App and TypeScript

From Dev

Typescript returns undefined on object

From Dev

AS_IF and AC_MSG_ERROR: error: possibly undefined macro

From Dev

Typescript Interface Optional Method Property Undefined

From Dev

Basic chaining method is returning object of class error

From Dev

Optional Chaining - Function.prototype.apply was called on undefined, which is an undefined and not a function

From Java

Cannot invoke an object which is possibly 'undefined'.ts(2722)

From Dev

autoconf: error: possibly undefined macro: AC_LIBOBJ

From Dev

error: possibly undefined macro: AC_PREFIX_CONFIG_H

From Java

Optional chaining (?.) in nashorn

From Dev

Optional Chaining returning an Int

From Dev

Swift 3.0 Optional Chaining

Related Related

  1. 1

    Access optional keys in object, where key is a literal in a literal union type without error "Object is possibly 'undefined'"

  2. 2

    Object is possibly 'undefined' when init a variable Typescript

  3. 3

    Typescript: Object is possibly 'undefined' but only for '<' and '>' operators

  4. 4

    Typescript, how to pass "Object is possibly null" error?

  5. 5

    Typescript optional chaining error: Expression expected.ts(1109)

  6. 6

    Typescript : Object is possibly null

  7. 7

    How to suppress "error TS2533: Object is possibly 'null' or 'undefined'"?

  8. 8

    Object is possibly null error Typescript when doing comparison

  9. 9

    Object is possibly 'undefined' in ternary operation

  10. 10

    Error: Call to a possibly undefined method

  11. 11

    Typescript/React: "Object is possibly 'null'"

  12. 12

    Typescript/React: "Object is possibly 'null'"

  13. 13

    How to use optional chaining with array in Typescript?

  14. 14

    How can I solve the error 'TS2532: Object is possibly 'undefined'?

  15. 15

    Object is possibly undefined during ternary operator

  16. 16

    typescript error TS2531 Object is possibly 'null' is NOT being suppressed by strictNullChecks : false

  17. 17

    Prop is possibly undefined error in Vue 3 setup

  18. 18

    How to enable optional chaining with Create React App and TypeScript

  19. 19

    Typescript returns undefined on object

  20. 20

    AS_IF and AC_MSG_ERROR: error: possibly undefined macro

  21. 21

    Typescript Interface Optional Method Property Undefined

  22. 22

    Basic chaining method is returning object of class error

  23. 23

    Optional Chaining - Function.prototype.apply was called on undefined, which is an undefined and not a function

  24. 24

    Cannot invoke an object which is possibly 'undefined'.ts(2722)

  25. 25

    autoconf: error: possibly undefined macro: AC_LIBOBJ

  26. 26

    error: possibly undefined macro: AC_PREFIX_CONFIG_H

  27. 27

    Optional chaining (?.) in nashorn

  28. 28

    Optional Chaining returning an Int

  29. 29

    Swift 3.0 Optional Chaining

HotTag

Archive