-
Notifications
You must be signed in to change notification settings - Fork 0
/
00472-hard-tuple-to-enum-object.ts
100 lines (95 loc) · 1.97 KB
/
00472-hard-tuple-to-enum-object.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// ============= Test Cases =============
import type { Equal, Expect } from './test-utils'
const OperatingSystem = ['macOS', 'Windows', 'Linux'] as const
const Command = [
'echo',
'grep',
'sed',
'awk',
'cut',
'uniq',
'head',
'tail',
'xargs',
'shift'
] as const
type cases = [
Expect<Equal<Enum<[]>, {}>>,
Expect<
Equal<
Enum<typeof OperatingSystem>,
{
readonly MacOS: 'macOS'
readonly Windows: 'Windows'
readonly Linux: 'Linux'
}
>
>,
Expect<
Equal<
Enum<typeof OperatingSystem, true>,
{
readonly MacOS: 0
readonly Windows: 1
readonly Linux: 2
}
>
>,
Expect<
Equal<
Enum<typeof Command>,
{
readonly Echo: 'echo'
readonly Grep: 'grep'
readonly Sed: 'sed'
readonly Awk: 'awk'
readonly Cut: 'cut'
readonly Uniq: 'uniq'
readonly Head: 'head'
readonly Tail: 'tail'
readonly Xargs: 'xargs'
readonly Shift: 'shift'
}
>
>,
Expect<
Equal<
Enum<typeof Command, true>,
{
readonly Echo: 0
readonly Grep: 1
readonly Sed: 2
readonly Awk: 3
readonly Cut: 4
readonly Uniq: 5
readonly Head: 6
readonly Tail: 7
readonly Xargs: 8
readonly Shift: 9
}
>
>
]
// ============= Your Code Here =============
type IndexOf<T, U, Count extends '🐔'[] = []> = T extends readonly [
infer Head,
...infer Tail
]
? Head extends U
? Count['length']
: IndexOf<Tail, U, [...Count, '🐔']>
: never
type Enum<
T extends readonly string[],
N extends boolean = false
> = N extends true
? {
readonly [P in T[number] as P extends string
? Capitalize<P>
: never]: IndexOf<T, P>
}
: {
readonly [P in T[number] as P extends string ? Capitalize<P> : never]: P
}
type Test = Enum<typeof OperatingSystem>
type Test2 = Enum<typeof Command, true>