This (`?:`) Typescript ternary operator

I am new to angular 2 and typescript. I see

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

in some typescript examples. What does this ( ?: Mean? Is it a ternary operator (only with a false condition) or some other?

Thanks in advance.

+5
source share
3 answers

The operator ? indicates that the property may be nullable / optional. It just means that the compiler does not throw an error if you do not implement this property in your implementation.

+10
source

The Elvis operator is available only to. not for other dereference operators, such as [].

As a workaround, use

 {{ data?.record ? data.record['name/first'] : null}} 
+1
source
  • Your code is nullable variable declaration

But character ?: Using the Elvis operator

The code looks like

 let displayName = user.name ?: ""; 

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

Read more: Comparison of the Ternary operator, Elvis operator, safe navigation operator and OR logical operators

+1
source

Source: https://habr.com/ru/post/1266640/


All Articles