【发布时间】:2020-10-01 12:02:58
【问题描述】:
根据
https://www.typescriptlang.org/docs/handbook/utility-types.html#nonnullabletype
例子NonNullable
通过从 Type 中排除 null 和 undefined 来构造一个类型。
type T0 = NonNullable<string | number | undefined>;
// ^ = type T0 = string | number
type T1 = NonNullable<string[] | null | undefined>;
// ^ = type T1 = string[]
我读过它被定义为:
type NonNullable<T> = T extends null ? never : T
现在,我想排除 null,但要包含我的代码所需的 undefined。
你会怎么做?谢谢。
【问题讨论】:
标签: typescript nullable