Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript: What's the meaning of a '!' at the end of an attribution

Tags:

typescript

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 :(

like image 987
taigi100 Avatar asked Apr 28 '17 11:04

taigi100


People also ask

What is '!' In TypeScript?

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 !

What does a mean at the end in TypeScript?

It tells the compiler that undefined is not undefined See stackoverflow.com/a/42274019/450611.

What is !: operator in TypeScript?

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 .

What does it mean to write two plus signs after a number type variable Javascript?

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.


2 Answers

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.

like image 152
taigi100 Avatar answered Oct 18 '22 06:10

taigi100


This post-fix expression operator ! may be used to assert that its operand cannot be null or undefined during runtime.

like image 2
karim somai Avatar answered Oct 18 '22 05:10

karim somai