NonNullable<T> loại bỏ null và undefined khỏi type T: NonNullable<string | null | undefined> cho ra string.
- Tương đương với
Exclude<T, null | undefined>. - Hay dùng sau khi đã kiểm tra null ở runtime nhưng TypeScript vẫn chưa tự narrow được — ví dụ kết quả từ
Array.find()có typeT | undefined, sau khi guard check có thể cast vềNonNullable. - Với strictNullChecks bật, đây là utility cần thiết để làm việc với optional values an toàn.
NonNullable<T> removes null and undefined from type T: NonNullable<string | null | undefined> gives string.
- Equivalent to
Exclude<T, null | undefined>. - Often used after null checking at runtime when TypeScript has not automatically narrowed the type — for example,
Array.find()returnsT | undefined, and after a guard check you can cast it toNonNullable. - With strictNullChecks enabled, this utility is essential for safely working with optional values.