Search code examples
angulartypescript

Why is typescript converting this nullish coallesce to a ternary?


I am working on an Angular app and I have the following code:

    // ...
    this.x = y ?? z;
    // ...

When I run ng serve I get the following error in my terminal:

error TS1109: Expression expected.
error TS1005: ':' expected.

Module parse failed: Unexpected token (47:40)
You may need an appropriate loader to handle this file type.
|         // ...
|         this.x = y ?  ? z :  : ;
|         // ...

Why is it converting the nullish coallesce (y ?? z) to a ternary (y ? ? z : : )?

I have tried adding brackets around the nullish coallesce but it doesn't change the output.


Solution

  • Ensure that you are using a TypeScript version that supports the nullish coalescing operator. The nullish coalescing operator was introduced in TypeScript 3.7.