Skip to content

Latest commit

 

History

History
41 lines (31 loc) · 899 Bytes

File metadata and controls

41 lines (31 loc) · 899 Bytes

Home > types-kit > ConditionalKeys

ConditionalKeys type

Get keys by Condition (value).

Signature:

export type ConditionalKeys<T, Condition, Exact extends boolean = false> = {
  [K in Keys<T>]: Exact extends true
    ? T[K] extends Condition
      ? K
      : never
    : T[K] extends infer V
    ? V extends Condition
      ? K
      : never
    : never
}[Keys<T>]

References: Keys

Example

 interface Props {
   a?: number
   b: string
   c: boolean
 }

 // Expect: 'b' | 'c'
 type PropKeys = ConditionalKeys<Props, string | boolean>
 // Set exact true, expect: 'c'
 type PropKeys2 = ConditionalKeys<Props, string | boolean, true>