/
array.ts
61 lines (56 loc) · 1.32 KB
/
array.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
52
53
54
55
56
57
58
59
60
61
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Extracts the type info for all items inside an array or tuple.
*
* @example
*
* ```ts
* type Values = ValuesInside<[true, 1, '2']>;
* // => true | 1 | "2"
* ```
*/
export type ValuesInside<T extends readonly unknown[]> = T extends ReadonlyArray<infer R> ? R : never;
/**
* Extracts the type info for the first item inside a tuple.
*
* @example
*
* ```ts
* type Value = Head<[true, 1, '2']>;
* // => true
* ```
*/
export type Head<T extends readonly unknown[]> = T extends [infer R, ...infer _] ? R : never;
/**
* Extracts the type info for all but the first item inside a tuple.
*
* @example
*
* ```ts
* type Values = Tail<[true, 1, '2']>;
* // => [1, "2"]
* ```
*/
export type Tail<T extends unknown[]> = T extends [infer _, ...infer R] ? R : never;
/**
* Extracts the type info for the last item inside a tuple.
*
* @example
*
* ```ts
* type Value = Last<[true, 1, '2']>;
* // => "2"
* ```
*/
export type Last<T extends readonly unknown[]> = T extends [...infer _, infer R] ? R : never;
/**
* Extracts the type info for all but the last item inside a tuple.
*
* @example
*
* ```ts
* type Values = Init<[true, 1, '2']>;
* // => [true, 1]
* ```
*/
export type Init<T extends readonly unknown[]> = T extends [...infer R, infer _] ? R : never;