I've seen in some React/TypeScript implementations such as :
ref={ ref => this.container = ref! }
What does the exclamation point means in ref!
? Is that something specific in TypeScript, or a new standard JavaScript notation?
The exclamation mark (non-null assertion) operator removes null and undefined from the type of an expression. It is used when we we know that a variable that TypeScript thinks could be null or undefined actually isn't. index.ts. Copied!
operator twice. It converts a nonboolean to an inverted boolean (for instance, ! 5 would be false, since 5 is a non-false value in JS), then boolean-inverts that so you get the original value as a boolean (so !! 5 would be true).
! is a logic reversal operator, if something was true it will change it to false, if something is false, it will change to true. example, we know that empty string or 0 in boolean is false.
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 !
In TypeScript, a postfix !
removes null
and undefined
from the type of an expression.
This is useful when you know, for reasons outside TypeScript's inference ability, that a variable that "could" be null
or undefined
actually isn't.
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