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?