RequireAtLeastOne
自 1.3.0 起
要求对象类型中指定的键至少存在一个。
签名
typescript
type RequireAtLeastOne<T, K extends keyof T = keyof T> = K extends K
? Omit<T, K> & { [P in K]-?: T[P] } & { [P in Exclude<K, K>]+?: T[P] }
: never参数
| 参数 | 描述 |
|---|---|
T | 对象类型 |
K | 必须至少存在一个的键(默认为所有键) |
示例
基本用法
typescript
import type { RequireAtLeastOne } from 'uni-types'
type Contact = { email?: string; phone?: string; address?: string }
type ValidContact = RequireAtLeastOne<Contact, 'email' | 'phone'>
// 必须至少有 email 或 phone,address 保持可选默认键
typescript
type Options = { a?: number; b?: string; c?: boolean }
type AtLeastOne = RequireAtLeastOne<Options>
// 必须至少有 a、b 或 c 中的一个API 响应
typescript
type SearchResult = { id?: string; name?: string; code?: number }
type MatchResult = RequireAtLeastOne<SearchResult, 'id' | 'name'>
// 必须通过 id 或 name 匹配相关
RequireExactlyOne- 要求恰好一个键RequireKeys- 将特定键设为必需MakeOptional- 将特定键设为可选