#usage of ! & ? in validation statements.

10 messages · Page 1 of 1 (latest)

pure tulip
#

*ngIf = "registerForm.controls.['email'].errors!['emailError']"

I have seen the usage of ! and ? at some places during validations in reactive forms. is it to check null or what is the purpose of those operators. Please explain if anybody has answer

pure tulip
#

I know null assertion and ternary operator but what is the purpose of using it in this

zealous belfry
#

The optional chaining (?.) operator accesses an object's property or calls a function. If the object is undefined or null, it returns undefined instead of throwing an error.

worn kite
#

! is to bypass types that might have undefined or null value.

zealous belfry
#

TypeScript also has a special syntax for removing null and undefined from a type without doing any explicit checking. Writing ! after any expression is effectively a type assertion that the value isn’t null or undefined

pure tulip
#

Ok thanks for the info.

worn kite
#

You can google ! vs ? typescript. Google is your best teacher

remote jolt
#

You can also just read the links I posted, which have nothing to do with the ternary operator and explain what you asked for.

zealous belfry
#

I just copied and pasted the first lines from each of those links and added bolding. 🙂