Repository navigation
Expand file tree
/
Copy pathunion-to-tuple.d.ts
More file actions
54 lines (40 loc) 路 1.8 KB
/
Copy pathunion-to-tuple.d.ts
File metadata and controls
54 lines (40 loc) 路 1.8 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
import type {ExcludeExactly} from './exclude-exactly.d.ts';
import type {IsNever} from './is-never.d.ts';
import type {TupleToUnion} from './tuple-to-union.d.ts'; // For `@link`
import type {UnionMember} from './union-member.d.ts';
import type {UnknownArray} from './unknown-array.d.ts';
/**
Convert a union type into an unordered tuple type of its elements.
"Unordered" means the elements of the tuple are not guaranteed to be in the same order as in the union type. The arrangement can appear random and may change at any time.
Because of this, it cannot be used to make sure a hand-written array lists every member of the union. For that, define the array first and derive the union with {@link TupleToUnion}, or list the members as object keys with `satisfies Record<Union, true>` and use `Object.keys()`.
This can be useful when you have objects with a finite set of keys and want a type defining only the allowed keys, but do not want to repeat yourself.
@example
```
import type {UnionToTuple} from 'type-fest';
type Numbers = 1 | 2 | 3;
type NumbersTuple = UnionToTuple<Numbers>;
//=> [1, 2, 3]
```
@example
```
import type {UnionToTuple} from 'type-fest';
const pets = {
dog: '馃惗',
cat: '馃惐',
snake: '馃悕',
};
type Pet = keyof typeof pets;
//=> 'dog' | 'cat' | 'snake'
const petList = Object.keys(pets) as UnionToTuple<Pet>;
//=> ['dog', 'cat', 'snake']
```
@see {@link TupleToUnion}
@category Array
*/
export type UnionToTuple<Union> =
_UnionToTuple<Union> extends infer Result extends UnknownArray ? Result : never; // Nudges the compiler that `UnionToTuple` always yields an array.
type _UnionToTuple<Union, Accumulator extends UnknownArray = [], Member = UnionMember<Union>> =
IsNever<Union> extends true
? Accumulator
: _UnionToTuple<ExcludeExactly<Union, Member>, [Member, ...Accumulator]>;
export {};