Nhảy tới nội dung

NonNullable<T>

NonNullable<T> là utility type trả về union type sau khi loại bỏ nullundefined khỏi union type T.

Mặc dù tên là NonNullable nhưng cũng có thể loại bỏ undefined.

Type argument của NonNullable<T>

T

Type argument T nhận union type muốn loại bỏ nullundefined.

Ví dụ sử dụng NonNullable

ts
type String1 = NonNullable<string>;
type String1 = string
type String2 = NonNullable<string | null>;
type String2 = string
type String3 = NonNullable<string | undefined>;
type String3 = string
type String4 = NonNullable<string | null | undefined>;
type String4 = string
ts
type String1 = NonNullable<string>;
type String1 = string
type String2 = NonNullable<string | null>;
type String2 = string
type String3 = NonNullable<string | undefined>;
type String3 = string
type String4 = NonNullable<string | null | undefined>;
type String4 = string

NonNullable<null>NonNullable<undefined> trở thành kiểu never:

ts
type Never1 = NonNullable<null>;
type Never1 = never
type Never2 = NonNullable<undefined>;
type Never2 = never
ts
type Never1 = NonNullable<null>;
type Never1 = never
type Never2 = NonNullable<undefined>;
type Never2 = never