forked from sindresorhus/type-fest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuple-to-union.d.ts
51 lines (36 loc) · 1.65 KB
/
tuple-to-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
43
44
45
46
47
48
49
50
51
/**
Convert a tuple/array into a union type of its elements.
This can be useful when you have a fixed set of allowed values and want a type defining only the allowed values, but do not want to repeat yourself.
@example
```
import type {TupleToUnion} from 'type-fest';
const destinations = ['a', 'b', 'c'] as const;
type Destination = TupleToUnion<typeof destinations>;
//=> 'a' | 'b' | 'c'
function verifyDestination(destination: unknown): destination is Destination {
return destinations.includes(destination as any);
}
type RequestBody = {
deliverTo: Destination;
};
function verifyRequestBody(body: unknown): body is RequestBody {
const deliverTo = (body as any).deliverTo;
return typeof body === 'object' && body !== null && verifyDestination(deliverTo);
}
```
Alternatively, you may use `typeof destinations[number]`. If `destinations` is a tuple, there is no difference. However if `destinations` is a string, the resulting type will the union of the characters in the string. Other types of `destinations` may result in a compile error. In comparison, TupleToUnion will return `never` if a tuple is not provided.
@example
```
const destinations = ['a', 'b', 'c'] as const;
type Destination = typeof destinations[number];
//=> 'a' | 'b' | 'c'
const erroringType = new Set(['a', 'b', 'c']);
type ErroringType = typeof erroringType[number];
//=> Type 'Set<string>' has no matching index signature for type 'number'. ts(2537)
const numberBool: { [n: number]: boolean } = { 1: true };
type NumberBool = typeof numberBool[number];
//=> boolean
```
@category Array
*/
export type TupleToUnion<ArrayType> = ArrayType extends readonly unknown[] ? ArrayType[number] : never;