24

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?

IAfanasov
  • 4,775
  • 3
  • 27
  • 42
Siva Pasupathi
  • 494
  • 1
  • 3
  • 15

4 Answers4

24

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.

LoïcR
  • 4,940
  • 1
  • 34
  • 50
14

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

9
  • 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

Community
  • 1
  • 1
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
4

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}}
Yoav Schniederman
  • 5,253
  • 3
  • 28
  • 32