Marty Pitt Marty Pitt - 1 month ago 10
TypeScript Question

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

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).

Answer

I can't find any reference to it whatsoever in the TypeScript language specification.

As far as what to call this operator in CoffeeScript, it's called the existential operator (specifically, the "accessor variant" of the existential operator).

From CoffeeScript's documentation on Operators:

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.

So, the accessor variant of the existential operator appears to be the proper way to refer to this operator; and TypeScript does not currently appear to support it (although others have expressed a desire for this functionality).

Comments