Câu hỏi phỏng vấn Typescript
Câu hỏi

What is the fundamental difference between Optional Chaining ( ?. ) and Non-null assertion operator ( ! )
in TypeScript?

Câu trả lời

The fundamental difference between Optional Chaining (?.) and the Non-null assertion operator (!) in TypeScript lies in how they handle null and undefined values during property access or method invocation.

Optional Chaining (?.) is a feature that allows you to safely access deeply nested properties of an object without having to check for the existence of each level in the chain. If at any point in the chain the value is null or undefined, the expression short-circuits and returns undefined immediately, without evaluating the rest of the chain. This prevents runtime errors when attempting to access a property or method of null or undefined[1][2][6].

On the other hand, the Non-null assertion operator (!) is used to tell TypeScript's type checker to trust you that a value is not null or undefined. It does not perform any runtime checks. Instead, it simply removes null and undefined from the type of the operand, allowing you to access properties or call methods without TypeScript raising type errors. However, if the value is actually null or undef...

middle

middle

Gợi ý câu hỏi phỏng vấn

senior

What is dynamic import expression?

junior

What is Interface in TypeScript?

middle

What is the default access modifier for members of a class in TypeScript?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào