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

entry

What is the difference between .ts and .tsx extensions in TypeScript?

entry

How to call base class constructor from child class in TypeScript?

middle

What are some use cases of template literal types in TypeScript?

Bình luận

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

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