Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it (`?:`) typescript ternary operator [duplicate]

Tags:

typescript

I saw the code below in a TypeScript example:

export interface EjectTaskOptions extends BuildOptions {
  force?: boolean;
  app?: string;
}

What does ?: mean? Is it a ternary operator (with only false condition) or something else?

like image 648
Siva Pasupathi Avatar asked Apr 12 '17 11:04

Siva Pasupathi


4 Answers

The ? operator indicate that the property can be nullable / optional. It just means that the compilator will not throw an error if you do not implement this property in your implementation.

like image 160
LoïcR Avatar answered Nov 02 '22 13:11

LoïcR


You can use ?? operator!

const test: string = null;

console.log(test ?? 'none');

This will print 'none' because test is null. If there is a value for test, it will print that. You can try that here playground typescript

like image 30
Sasindu Lakshitha Avatar answered Nov 02 '22 15:11

Sasindu Lakshitha


  • Your code is nullable variable declaration

But the symbol of ?: using from Elvis operator

It Code looks like

let displayName = user.name ?: "";

And it's not available in typescript/javascript/angular and essentially the same as ||

More details : Comparison of Ternary operator, Elvis operator, safe Navigation Operator and logical OR operators

like image 9
Ramesh Rajendran Avatar answered Nov 02 '22 14:11

Ramesh Rajendran


The Elvis operator is only available for the . not for other dereference operators like [].

As a workaround use

{{ data?.record ? data.record['name/first'] : null}}
like image 4
Yoav Schniederman Avatar answered Nov 02 '22 14:11

Yoav Schniederman