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