What is the difference between the following declarations?
loginForm!: FormGroup;
loginForm: FormGroup | undefined;
What is the difference between the following declarations?
loginForm!: FormGroup;
loginForm: FormGroup | undefined;
When you just declare a variable without initialized, you get will an error from editor itself, something like this:
angular Type 'null | undefined' is not assignable to type 'FormGroup | undefined' or initialized variable in constructor
This happen due to typescript version 2.1 or above.
If you want to prevent from such error, you have to use "Definite Assignment Assertion" feature, with your variable or you have to initialized the variable within constructor,
'Definite Assignment Assertion' or '!' is a feature in typescript, and it also called non-null assertion operator. It will tell typescript that this variable will have a value at runtime.
Another approach which you mentioned in your question, is not a recommended approach, it may prevent you from runtime error but it doesn't make a sense to provide the undefined parallels to Type