Elias
Elias

Reputation: 453

What exactly is the difference between ?. and !. in Angular?

I've got the next line of code in my html template file:


    <span *ngIf="!signupForm.get('email')!.valid && signupForm.get('email')?.touched">

The 1st condition of the if has !. and the 2nd condition has ?. .

Both works just fine and when I don't use ? or ! it gives me the error: Object is possibly 'null'.

So does it really matter which one I use?

Upvotes: 0

Views: 1290

Answers (1)

meriton
meriton

Reputation: 70574

!. is TypeScript's null assertion operator, which asserts that the programmer knows the value to be not null, and asks typescript to believe the programmer. Should the value be null anyway, you'll get an exception.

?. is EcmaScript's optional chaining, which will access the property if the base is defined, or return undefined otherwise.

Upvotes: 2

Related Questions