Skip to content

NonNullable

自 1.0.0 起

从类型中排除 nullundefined

签名

typescript
type NonNullable<T> = T & {}

参数

参数描述
T要使其不可为 null 的类型

描述

从类型中移除 nullundefined。这与 TypeScript 内置的 NonNullable 类似,但实现方式不同以获得更广泛的兼容性。

示例

基本用法

typescript
import type { NonNullable } from 'uni-types'

type Clean = NonNullable<string | null | undefined> // string
type Clean2 = NonNullable<number | null> // number

与对象类型一起使用

typescript
interface User {
  name: string | null
  email: string | undefined
}

type CleanUser = NonNullable<User>
// User(null/undefined 仍保留在属性中,如需深度移除请使用 NoNullish)

相关

基于 MIT 许可发布