Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I combine destructuring assignment and optional chaining?

I have a TypeScript interface with some optional fields and a variable of that type:

interface Foo {     config?: {         longFieldName?: string;     } }  declare let f: Foo; 

I'd like to put longFieldName in a variable of the same name.

If config weren't optional, I'd use destructuring assignment to do this without repeating longFieldName. But it is, so I get a type error:

const { longFieldName } = f.config;      // ~~~~~~~~~~~~~  Property 'longFieldName' does not exist on type '{ longFieldName?: string | undefined; } | undefined'. 

I can use optional chaining to concisely handle the undefined case:

const longFieldName = f?.config.longFieldName;  // OK, type is string | undefined 

But now I have to repeat longFieldName.

Can I have my cake and eat it, too? Can I use optional chaining to handle the undefined case without repeating longFieldName? If not, what is the most concise/idiomatic workaround? See playground link.

like image 578
danvk Avatar asked Nov 14 '19 21:11

danvk


People also ask

Can you Destructure array of objects?

To destructure an array in JavaScript, we use the square brackets [] to store the variable name which will be assigned to the name of the array storing the element.

Does JavaScript support optional chaining?

Introduction to the JavaScript optional chaining operator allows you to access the value of a property located deep within a chain of objects without explicitly checking if each reference in the chain is null or undefined .

Does Destructuring create a shallow copy?

No, destructuring will give you a reference. AFAIK, there is no way to configure a destructure to shallow clone at the same time.

What is the optional chaining operator in a connected object?

The optional chaining operator ( ?. ) enables you to read the value of a property located deep within a chain of connected objects without having to check that each reference in the chain is valid.

Is it possible to combine array destructuring and object destructuring?

Array and Object destructuring can be combined. Say you want the third element in the array props below, and then you want the name property in the object, you can do the following: When deconstructing an object, if a property is not accessed in itself, it will continue to look up along the prototype chain.

What is destructuring assignment syntax?

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables. The source for this interactive example is stored in a GitHub repository.

Can I use optional chaining with an undefined root object?

Optional chaining cannot be used on a non-declared root object, but can be used with an undefined root object. The optional chaining operator provides a way to simplify accessing values through connected objects when it's possible that a reference or function may be undefined or null .

What is a destructive assignment in C++?

“Destructuring” does not mean “destructive”. It’s called “destructuring assignment,” because it “destructurizes” by copying items into variables. But the array itself is not modified. Unwanted elements of the array can also be thrown away via an extra comma:


1 Answers

Use short circuit evaluation to get a fallback value (empty object) if the f?.config expression evaluate to undefined:

const { longFieldName } = f?.config || {}; 
like image 71
Ori Drori Avatar answered Sep 21 '22 08:09

Ori Drori