@@ -8,6 +8,12 @@ Typesafe string enums in TypeScript.
8
8
9
9
* [ Installation] ( #installation )
10
10
* [ Usage] ( #usage )
11
+ - [ Creating and using enums] ( #creating-and-using-enums )
12
+ - [ Additional functions] ( #additional-functions )
13
+ - [ Enum.isType(enum, value)] ( #enum-istype-enum-value )
14
+ - [ Enum.keys(enum)] ( #enum-keys-enum )
15
+ - [ Enum.values(enum)] ( #enum-values-enum )
16
+ - [ Enum.ofKeys(object)] ( #enum-ofkeys-object )
11
17
* [ Motivation] ( #motivation )
12
18
- [ Why not built-in enums?] ( #why-not-built-in-enums )
13
19
- [ Why not string literals?] ( #why-not-string-literals )
@@ -25,6 +31,8 @@ This library requires TypeScript 2.2 or later. If you require TypeScript 2.1 com
25
31
26
32
## Usage
27
33
34
+ ### Creating and using enums
35
+
28
36
Define an enum as follows:
29
37
30
38
``` javascript
@@ -89,8 +97,39 @@ export type Status = Enum<typeof Status>;
89
97
console .log (Status .RUNNING ); // -> "running"
90
98
```
91
99
92
- Several helper functions are provided. First are ` Enum.keys() ` and ` Enum.values() ` , which resemble
93
- ` Object.keys() ` and ` Object.values() ` but provide strict typing in their return type:
100
+ ### Additional functions
101
+
102
+ #### ` Enum.isType(enum, value) `
103
+
104
+ ` Enum.isType ` checks if a value is of a given enum type and can be used as a type guard. For example:
105
+
106
+ ``` javascript
107
+ const Color = Enum (" BLACK" , " WHITE" );
108
+ type Color = Enum< typeof Color> ;
109
+
110
+ let selectedColor: Color;
111
+ const colorString = getUserInputString (); // Unsanitized string.
112
+
113
+ // TypeScript error: Type 'string' is not assignable to type '"BLACK" | "WHITE"'.
114
+ selectedColor = colorString;
115
+
116
+ if (Enum .isType (Color, colorString)) {
117
+ // No error this time because within type guard.
118
+ selectedColor = colorString;
119
+ } else {
120
+ throw new Error (` ${ colorString} is not a valid color` );
121
+ }
122
+ ```
123
+
124
+ #### ` Enum.keys(enum) `
125
+
126
+ Resembles ` Object.keys() ` , but provides strict typing in its return type.
127
+
128
+ #### ` Enum.values(enum) `
129
+
130
+ Resembles ` Object.values() ` , but provides strict typing in its return type.
131
+
132
+ Example of both ` Enum.keys() ` and ` Enum.values() ` :
94
133
95
134
``` javascript
96
135
const FileType = Enum ({
@@ -109,24 +148,22 @@ const values = Enum.values(FileType);
109
148
// Return value: ["application/pdf", "text/plain", "image/jpeg"] (not necessarily in that order)
110
149
```
111
150
112
- Also available is ` Enum.isType() ` , which checks if a value is of a given enum type and can be used
113
- as a type guard.
151
+ #### Enum.ofKeys(object)
152
+
153
+ Creates a new enum with the same keys as the provided enum or object and whose values are equal to
154
+ its keys. This is most useful if for some reason it is necessary to do string comparisons against
155
+ the keys of an enum rather than the values. For example:
114
156
115
157
``` javascript
116
- const Color = Enum (" BLACK " , " WHITE " );
117
- type Color = Enum< typeof Color > ;
158
+ const ErrorColor = Enum ({ OK : " green " , ERROR : " red " } );
159
+ type ErrorColor = Enum< typeof ErrorColor > ;
118
160
119
- let selectedColor: Color;
120
- const colorString = getUserInputString (); // Unsanitized string.
161
+ const ErrorLevel = Enum .ofKeys (ErrorCodes);
121
162
122
- // TypeScript error: Type 'string' is not assignable to type '"BLACK" | "WHITE"'.
123
- selectedColor = colorString;
163
+ const errorLevel = getErrorLevel ();
124
164
125
- if (Enum .isType (Color, colorString)) {
126
- // No error because within type guard.
127
- selectedColor = colorString;
128
- } else {
129
- throw new Error (` ${ colorString} is not a valid color` );
165
+ if (errorLevel === ErrorLevel .ERROR ) {
166
+ ...
130
167
}
131
168
```
132
169
0 commit comments