Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does TypeScript have a Null-conditional operator

Is there any operator like ?. in TypeScript that can check if the variable is null or not defined like Kotlin? Like

person?.getName()?.firstName ?: "None"
like image 634
Da Yin Avatar asked Aug 01 '17 14:08

Da Yin


People also ask

Does TypeScript support null?

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 .

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.

Does TypeScript have optional chaining?

In TypeScript, optional chaining is defined as the ability to immediately stop running an expression if a part of it evaluates to either null or undefined .

What is ?: In TypeScript?

What does ?: mean in TypeScript? Using a question mark followed by a colon ( ?: ) means a property is optional. That said, a property can either have a value based on the type defined or its value can be undefined .


5 Answers

Yes, as of Typescript 3.7 you can now do this via optional-chaining

person?.getName()?.firstName

gets transpiled to

let firstName = person === null || person === void 0 ? void 0 : (_person$getName = person.getName()) === null || _person$getName === void 0 ? void 0 : _person$getName.firstName;

Note the check for null. This will work as expected if for example person is defined as

let person:any = null; //no runtime TypeError when calling person?.getName()

However if person is defined as

let person:any = {};//Uncaught TypeError: person.getName is not a function

See also this similar stackoverflow question

like image 115
wal Avatar answered Oct 22 '22 11:10

wal


No, as of now safe navigation operatior is still not implemented in Typescript: https://github.com/Microsoft/TypeScript/issues/16

However according to the latest standardization meeting notes it has been proposed, so maybe v3 :)

https://github.com/tc39/agendas/blob/master/2017/07.md

like image 21
Loonquawl Avatar answered Oct 22 '22 13:10

Loonquawl


I've encountered the same situation, and the following worked for me.

First, I defined a separate class with a property that can be null:

export class Foo {
    id: number; //this can be null
}

Then in my main class I set a property foo to this new type:

foo: Foo

After that, I was able to use it like this:

var fooId = (this.foo || ({} as Foo)).id;

This is the closest I could get to have a null propagation in a current version of TypeScript.

like image 7
Andrii Stashko Avatar answered Oct 22 '22 11:10

Andrii Stashko


Actually this is related to javascript null safety discussion that is mentioned in this answer. I guess they want it to be supported on javascript before they will get to typescript.

like image 3
ApriOri Avatar answered Oct 22 '22 12:10

ApriOri


I used this in my code to check for null

this.firstname = (this.firstname != null) ? this.firstname : this.firstname;

This could do till v3 is out

like image 3
Ebuka Avatar answered Oct 22 '22 13:10

Ebuka