6

I get response registryReportSettings from server:

this.getRegistrySettings(registry.Id).subscribe((registryReportSettings: { extended: ReportPropertiesRequest }) => {
  const { objectProperties, reportProperties, textProperties } = registryReportSettings?.extended;
}

If it is null I get an error:

TypeError: Cannot destructure property 'objectProperties' of '(intermediate value)(intermediate value)(intermediate value)' as it is undefined.

How to fix it using TS?

1
  • Start by properly declaring the type to match the values you're actually getting: { extended: ReportPropertiesRequest | null } Commented Sep 24, 2021 at 2:07

1 Answer 1

9

The error means that you try to deconstruct a possible undefined (Because you use optional chaining)

You can do

const { objectProperties, reportProperties, textProperties } = registryReportSettings?.extended ?? {};

So you will always deconstruct an object

Sign up to request clarification or add additional context in comments.

2 Comments

Consider using nullish coalescing operator ?? instead of ||. In this context it wouldn't make that much of a difference but as soon as you are dealing with primitive values, it will.
You right, I've updated my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.