Skip to content

AssertNotNil

自 1.3.0 起

断言类型 T 不是 never

签名

typescript
type AssertNotNil<T> = [T] extends [never] ? never : T

参数

参数描述
T要检查的类型

示例

基本用法

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

type Result = AssertNotNil<string> // string
type Failed = AssertNotNil<never> // never

条件类型

typescript
type MaybeString = string | undefined
type Validated = AssertNotNil<Exclude<MaybeString, undefined>> // string

推断类型

typescript
type ExtractArray<T> = T extends (infer E)[] ? E : never
type Result = AssertNotNil<ExtractArray<string[]>> // string

相关

基于 MIT 许可发布