| 1 | import type {UnionToIntersection} from './union-to-intersection.d.ts'; |
| 2 | |
| 3 | /** |
| 4 | Create a union of all keys from a given type, even those exclusive to specific union members. |
| 5 | |
| 6 | Unlike the native `keyof` keyword, which returns keys present in **all** union members, this type returns keys from **any** member. |
| 7 | |
| 8 | @link https://stackoverflow.com/a/49402091 |
| 9 | |
| 10 | @example |
| 11 | ``` |
| 12 | import type {KeysOfUnion} from 'type-fest'; |
| 13 | |
| 14 | type A = { |
| 15 | common: string; |
| 16 | a: number; |
| 17 | }; |
| 18 | |
| 19 | type B = { |
| 20 | common: string; |
| 21 | b: string; |
| 22 | }; |
| 23 | |
| 24 | type C = { |
| 25 | common: string; |
| 26 | c: boolean; |
| 27 | }; |
| 28 | |
| 29 | type Union = A | B | C; |
| 30 | |
| 31 | type CommonKeys = keyof Union; |
| 32 | //=> 'common' |
| 33 | |
| 34 | type AllKeys = KeysOfUnion<Union>; |
| 35 | //=> 'common' | 'a' | 'b' | 'c' |
| 36 | ``` |
| 37 | |
| 38 | @category Object |
| 39 | */ |
| 40 | export type KeysOfUnion<ObjectType> = |
| 41 | // Hack to fix https://github.com/sindresorhus/type-fest/issues/1008 |
| 42 | keyof UnionToIntersection<ObjectType extends unknown ? Record<keyof ObjectType, never> : never>; |
| 43 | |
| 44 | export {}; |