Skip to content

OptionalKeys ​

Since 1.0.0

Get keys of optional properties.

Signature ​

typescript
type OptionalKeys<T> = {
  [K in keyof T]-?: {} extends Pick<T, K> ? K : never
}[keyof T]

Parameters ​

ParameterDescription
TThe object type

Description ​

Extracts the keys of all optional properties from an object type.

Examples ​

Basic Usage ​

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

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

type Optional = OptionalKeys<User>
// 'age' | 'phone'

All Required ​

typescript
interface Required {
  a: string
  b: number
}

type NoOptional = OptionalKeys<Required> // never

Released under the MIT License.