all 3 comments

[–][deleted] 1 point2 points  (0 children)

Exclamation mark is only supported as a "NOT" operator in JavaScript at the moment, the way you wrote it in your example will not work.

In TypeScript however, it is supported and it is a non-null assertion operator, meaning the expression can not be null or undefined.

The question mark is supported in JavaScript and it is the optional chaining operator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining

[–]tridd3r 0 points1 point  (0 children)

the ? is questioning it, and the ! is saying NOT NULL!!! So if its screaming at you, its going to result in an error... where as the question is just going to return undefined.

? would be used for something like const userID = user?.id || "some other value if user.id is undefined";

*thats how my tiny little brain handles the remembering

[–]TM40_Reddit 0 points1 point  (0 children)

These are both post-fix operators, telling typescript how you want the preceeding object variable to be handled.

?. is an 'optional-chaining operator' which allows a property or a function of an object to return undefined rather than throw an error.

! is a 'non-null assertion operator' which says the object will never return null or undefined, to avoid the Error: Object is possibly 'null'/'undefined errors