Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacement of Elvis Operator of Angular2 in Typescript

Is there any operator in the typescript which is used similer to Elvis Operator of angular2, i mean to say let supppose i have to get key from object like this:

this.myForm.name.first_name 

and in case first_name does't exist so it will throw error first_name of undefined,

yes i can handel this error using Ternary operator of typescript like this

this.myForm.name ? this.myForm.name.first_name : '' 

but sometimes keys are getting too long,

so is there any operator like Elvis Operator of angular2 in the typescript so that i can use like this

this.myForm?.name?.first_name 
like image 278
Pardeep Jain Avatar asked Aug 31 '16 10:08

Pardeep Jain


People also ask

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 Elvis operator in angular?

The Safe Navigation Operator is also known as the "Elvis Operator". This operator is very useful to protect against null and undefined values in property paths. This operator allows us to navigate an object path in situations when we are not aware whether a path exists or not.

Does TypeScript support operator?

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 .

What is operator in TypeScript?

An Operator is a symbol which operates on a value or data. It represents a specific action on working with data. The data on which operators operates is called operand. It can be used with one or more than one values to produce a single value.


1 Answers

Update December 2019: TypeScript 3.7 introduced Optional Chaining which is equivalent to the safe navigation operator known in other languages. The ECMAScript proposal optional chaining has reached stage 4 and will thus be part of the specification in ES2020. See mdn: Optional chaining for more information.


Update July 2017: As JGFMK pointed out in the comments, there is an ECMAScript proposal called Optional Chaining for JavaScript. If/when the proposal reaches Stage 4, it will be added to the language specification.


There is neither a safe navigation nor elvis operator in TypeScript and, as far as I know, nothing comparable, either.

For a reference see the feature request at Suggestion: "safe navigation operator", i.e. x?.y. The explanation for not implementing it is the following (which, in my opinion, is a valid reason):

Closing this for now. Since there's not really anything TypeScript-specific that would require this at expression level, this kind of big operator change should happen at the ES spec committee rather than here.

The general tripwires for re-evaluating this would be a concrete ES proposal reaching the next stage, or a general consensus from the ES committee that this feature wouldn't happen for a long time (so that we could define our own semantics and be reasonably sure that they would "win").

Alternatives to that notation would be to use the logical AND operator, try/catch or a helper function like getSafe(() => this.myForm.name.first_name) as described in this post.

like image 63
str Avatar answered Sep 25 '22 13:09

str