You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Converts a union or tuple of property keys (string, number, or symbol) into an **Enum**-like object.
34
8
@@ -37,37 +11,39 @@ The keys are preserved, and their values are either:
37
11
- Their own literal values (by default)
38
12
- Or numeric indices (`1`, `2`, ...) if `Numeric` is `true`
39
13
40
-
By default, **Property** names are not **CamelCased** and **Numeric Enums** start from **Index `1`**. See {@link UnionToEnumOptions} to change this behaviour.
41
-
42
-
This is useful for creating strongly typed enums from a union of literals.
14
+
This is useful for creating strongly typed enums from a union/tuple of literals.
43
15
44
16
@example-types
45
17
```
18
+
import type {UnionToEnum} from 'type-fest';
19
+
46
20
type E1 = UnionToEnum<'A' | 'B' | 'C'>;
47
21
//=> { A: 'A'; B: 'B'; C: 'C' }
48
22
49
23
type E2 = UnionToEnum<'X' | 'Y' | 'Z', true>;
50
24
//=> { X: 1; Y: 2; Z: 3 }
51
25
52
-
type E3 = UnionToEnum<['Play', 'Pause', 'Stop'], true, {startIndex: 3}>;
53
-
//=> { Play: 3; Pause: 4; Stop: 5 }
26
+
type E3 = UnionToEnum<['Play', 'Pause', 'Stop'], true>;
27
+
//=> { Play: 1; Pause: 2; Stop: 3 }
54
28
55
-
type E4 = UnionToEnum<['some_key', 'another_key'], false, {camelCase: true}>;
0 commit comments