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?
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.
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
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
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}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With