1.

Explain how optional chaining works in TypeScript.

Answer»

Optional chaining allows you to ACCESS PROPERTIES and call methods on them in a chain-like fashion. You can do this using the ‘?.’ operator.

TypeScript immediately STOPS running some expression if it runs into a ‘null’ or ‘undefined’ value and RETURNS ‘undefined’ for the entire expression chain.

Using optional chaining, the following expression

let x = FOO === null || foo === undefined ? undefined : foo.bar.baz();

can be expressed as:

let x = foo?.bar.baz();


Discussion

No Comment Found