'What is the symbol ! mean in typescript when initializing a variable [duplicate]
I am working on angular project , while editing the component.ts file example given by my teacher . a line of code appears .
@Input() public searchType!: string;
It is inside the component typescript OnInit() function ,
I have no idea what the symbol ! means in this line of code and what is the effcts it brings .
As far as I know , it means not equals as an operator in language like php , java , but what does it mean when initializing a variable for typescript ?
Solution 1:[1]
! - aka TypeScript's "non-null assertion operator", is a way to tell typescript that you know for sure the value of a property is neither null nor undefined.
Note that this is potentially dangerous, since if the value does end up being null or undefined, it could result in unwanted behavior.
As you can infer, this is a way to "silence" typescript, and therefor should only be used if you are sure that it is correct for your use-case.
This is different to JavaScript's ?, aka "optional-chaining operator", which is a way of saying that the value might be undefined, and that in such a case, it should simply return an undefined.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
