Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Typescript support the ?. operator? (And, what's it called?)

Tags:

typescript

Does Typescript currently (or are there plans to) support the safe navigation operator of ?.

ie:

var thing = foo?.bar // same as: var thing = (foo) ? foo.bar : null; 

Also, is there a more common name for this operator (it's incedibly hard to google for).

like image 963
Marty Pitt Avatar asked Mar 07 '13 00:03

Marty Pitt


People also ask

What is the operator called in TypeScript?

TypeScript 3.7 added support for the ?? operator, which is known as the nullish coalescing operator. We can use this operator to provide a fallback value for a value that might be null or undefined .

Does TypeScript have the Elvis operator?

Update April 11, 2020: The Elvis operator has finally landed with TypeScript 3.7. You find it as optional chaining in the TypeScript 3.7 documentation.

What is the question mark operator in TypeScript?

The question mark dot (?.) syntax is called optional chaining in TypeScript and is like using dot notation to access a nested property of an object, but instead of causing an error if the reference is nullish, it short-circuits returning undefined . Copied!

Does TypeScript have null safety?

At some point while working with Angular we loved the Safe Navigation aka Elvis Operator (?.) in Angular Templates. As it helps us to forget about having extra null checks while using objects and just use that operator to handle the same for us.


2 Answers

Yes. As of TypeScript 3.7 (released on November 5, 2019), this feature is supported and is called Optional Chaining:

At its core, optional chaining lets us write code where TypeScript can immediately stop running some expressions if we run into a null or undefined. The star of the show in optional chaining is the new ?. operator for optional property accesses.

Refer to the TypeScript 3.7 release notes for more details.


Prior to version 3.7, this was not supported in TypeScript, although it was requested as early as Issue #16 on the TypeScript repo (dating back to 2014).

As far as what to call this operator, there doesn't appear to be a consensus. In addition to "optional chaining" (which is also what it's called in JavaScript), there are a couple of other examples:

  • CoffeeScript refers to it as the existential operator (specifically, the "accessor variant" of the existential operator):

The accessor variant of the existential operator ?. can be used to soak up null references in a chain of properties. Use it instead of the dot accessor . in cases where the base value may be null or undefined.

  • C# calls this a null-conditional operator.

a null-conditional operator applies a member access, ?., or element access, ?[], operation to its operand only if that operand evaluates to non-null; otherwise, it returns null.

  • Kotlin refers to it as the safe call operator.

There are probably lots of other examples, too.

like image 83
Donut Avatar answered Sep 30 '22 07:09

Donut


It is now possible, see answer of user "Donut".

Old answer: Standard JavaScript behaviour regarding boolean operators has something that may help. The boolean methods do not return true or false when comparing objects, but in case of OR the first value that is equal to true.

Not as nice as a single ?, but it works:

var thing = foo && foo.bar || null; 

You can use as many && as you like:

var thing = foo && foo.bar && foo.bar.check && foo.bar.check.x || null; 

Default values are also possible:

var name = person && person.name || "Unknown user"; 
like image 21
A. K-R Avatar answered Sep 30 '22 09:09

A. K-R