42

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"
Matze
  • 5,100
  • 6
  • 46
  • 69
Da Yin
  • 423
  • 1
  • 4
  • 6
  • Not to my knowledge, there's just a operator which tells the compiler that a value won't be `null`: (`person!.getName()`) – cyr_x Aug 01 '17 at 14:52
  • 4
    Possible duplicate of [Does Typescript support the ?. operator? (And, what's it called?)](https://stackoverflow.com/questions/15260732/does-typescript-support-the-operator-and-whats-it-called) – Daria Pydorenko Aug 01 '17 at 14:53
  • Yes. But I think this operator is just for compiler but does nothing to check. If the person is null in runtime, I suppose it will cause an error. Is is right? – Da Yin Aug 01 '17 at 15:01
  • Yes, you are right. When I search ?. in typescript, stackoverflow did not give that. Sorry for the duplication. – Da Yin Aug 01 '17 at 15:02
  • In HTML It is allowed but not in TS – Sreemat Aug 01 '17 at 18:48
  • This is now available as of TS 3.7, see https://stackoverflow.com/a/60594782/618441, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator – AndreasHassing Jun 03 '20 at 17:07

8 Answers8

25

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

wal
  • 17,409
  • 8
  • 74
  • 109
  • 2
    Similarly, [nullish coalescing operator](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#nullish-coalescing) also exists as of TS 3.7: `let x = foo ?? bar();` – AndreasHassing Jun 03 '20 at 17:09
18

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

Loonquawl
  • 1,058
  • 1
  • 11
  • 16
  • 1
    Thank you. I have looked through the handbook but did not find anything. I think this operator is useful and beautiful that eliminate a lot of shit code. Do you have any idea whether this operator is intended to be included in ES standards in the future? – Da Yin Aug 01 '17 at 14:58
  • As a side note typescript does have the ``?`` operator but does not do that. – Leonardo Chaia Aug 01 '17 at 14:58
  • 1
    For anyone looking, here is the [related javascript proposal](https://github.com/tc39/proposal-optional-chaining). – ProgrammingLlama Sep 04 '18 at 02:00
  • 1
    _so maybe v3_ v3 is out, maybe this answer needs to be updated (even if it is just to say it was not accepted if that is the case – santiago arizti Dec 06 '18 at 19:06
  • I'm using Typescript v3.7.2 but still not found this feature. could someone help please? – Franva Nov 15 '19 at 04:32
  • it is now possible see [my answer](https://stackoverflow.com/a/60594782/224410) – wal Jun 04 '20 at 00:37
  • been possible since 3.7.x version somewhere, and as of ES2020, also just available in javascript. – bvdb Jan 28 '21 at 21:06
7

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.

A.S.
  • 116
  • 2
  • 4
3

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.

ApriOri
  • 2,618
  • 29
  • 48
3

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

Ebuka
  • 616
  • 8
  • 13
3

The actual good answer is in Andreas' comment. Your example would be implemented as below from Typescript 3.7 onwards :

person?.getName()?.firstName ?? "None"

See https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#nullish-coalescing

Nicolas Seiller
  • 564
  • 1
  • 10
  • 20
1

Not only is it supported in typescript (since version 3.7.2). It is also supported in javascript now (since ES2020).

  • In practice that means you can use it in frameworks like Angular (since v9 which supports typescript 3.7).

  • But especially now that it is in the ES2020 spec, we will be seeing it everywhere.

bvdb
  • 22,839
  • 10
  • 110
  • 123
-1

This will set item to null if there is no e-mail:

let item = user?.email;
sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40