I was looking through angular2 code and saw a few things such as:
this._outlets[name] = undefined !;
What is the meaning of that !
at the end? Couldn't find anything on google about it :(
What is the TypeScript exclamation mark? The non-null assertion operator tells the TypeScript compiler that a value typed as optional cannot be null or undefined . For example, if we define a variable as possibly a string or undefined, the !
It tells the compiler that undefined is not undefined See stackoverflow.com/a/42274019/450611.
August 6, 2020. TypeScript 3.7 added support for the ?? operator, which is known as the nullish coalescing operator. We can use this operator to provide a fallback value for a value that might be null or undefined .
The addition assignment operator ( += ) adds the value of the right operand to a variable and assigns the result to the variable. The types of the two operands determine the behavior of the addition assignment operator.
After some checking I found out that it is indeed telling the compiler that undefined is not undefined
:)
In case you run the compiler with --strictNullChecks
trying to assign undefined to something such as a string for example will yielf in the following error: Type "undefined" is not assignable to type "string"
. If you use undefined !
you basically bypass this check and tsc won't give you an error for it.
This post-fix expression operator ! may be used to assert that its operand cannot be null or undefined during runtime.
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