Skip to content

PickPartial

自 1.0.0 起

将指定属性设置为可选。

签名

typescript
type PickPartial<T, K extends keyof T> = {
  [P in K]?: T[P]
} & Omit<T, K>

参数

参数描述
T目标类型
K要设置为可选的键(必须是 T 的键)

示例

基本用法

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

interface User {
  name: string
  age: number
  email: string
}

type OptionalEmail = PickPartial<User, 'email'>
// { name: string; age: number; email?: string }

多个属性

typescript
type OptionalMultiple = PickPartial<User, 'age' | 'email'>
// { name: string; age?: number; email?: string }

相关

基于 MIT 许可发布