File tree Expand file tree Collapse file tree 5 files changed +49
-7
lines changed Expand file tree Collapse file tree 5 files changed +49
-7
lines changed Original file line number Diff line number Diff line change @@ -85,8 +85,8 @@ export type Status = Enum<typeof Status>;
85
85
console .log (Status .RUNNING ); // -> "running"
86
86
```
87
87
88
- Two helper functions are provided: ` Enum.keys() ` and ` Enum.values() ` , which resemble ` Object.keys() `
89
- and ` Object.values() ` but provide strict typing in their return type:
88
+ Several helper functions are provided. First are ` Enum.keys() ` and ` Enum.values() ` , which resemble
89
+ ` Object.keys() ` and ` Object.values() ` but provide strict typing in their return type:
90
90
91
91
``` javascript
92
92
const FileType = Enum ({
@@ -105,6 +105,27 @@ const values = Enum.values(FileType);
105
105
// Return value: ["application/pdf", "text/plain", "image/jpeg"] (not necessarily in that order)
106
106
```
107
107
108
+ Also available is ` Enum.isType() ` , which checks if a value is of a given enum type and can be used
109
+ as a type guard.
110
+
111
+ ``` javascript
112
+ const Color = Enum ({
113
+ BLACK : " black" ,
114
+ WHITE : " white" ,
115
+ });
116
+ type Color = Enum< typeof Color> ;
117
+
118
+ let selectedColor: Color;
119
+
120
+ const colorString = getUserInputString (); // Unsanitized string.
121
+ if (Enum .isType (Color, colorString)) {
122
+ // Allowed because within type guard.
123
+ selectedColor = colorString;
124
+ } else {
125
+ throw new Error (` ${ colorString} is not a valid color` );
126
+ }
127
+ ```
128
+
108
129
## Motivation
109
130
110
131
Enums are useful for cleanly specifying a type that can take one of a few specific values.
Original file line number Diff line number Diff line change @@ -25,7 +25,7 @@ describe("Enum.keys", () => {
25
25
} ) ;
26
26
expect ( Enum . keys ( e ) ) . toEqual ( expect . arrayContaining ( [ "WHITE" , "BLACK" ] ) ) ;
27
27
} ) ;
28
- } ;
28
+ } ) ;
29
29
30
30
describe ( "Enum.values" , ( ) => {
31
31
it ( "returns the values of an enum object" , ( ) => {
@@ -36,3 +36,18 @@ describe("Enum.values", () => {
36
36
expect ( Enum . values ( e ) ) . toEqual ( expect . arrayContaining ( [ "white" , "black" ] ) ) ;
37
37
} ) ;
38
38
} ) ;
39
+
40
+ describe ( "Enum.isType" , ( ) => {
41
+ const Color = Enum ( {
42
+ BLACK : "black" ,
43
+ WHITE : "white" ,
44
+ } ) ;
45
+
46
+ it ( "returns true if value is of type" , ( ) => {
47
+ expect ( Enum . isType ( Color , "black" ) ) . toBe ( true ) ;
48
+ } ) ;
49
+
50
+ it ( "returns false if value is not of type" , ( ) => {
51
+ expect ( Enum . isType ( Color , "BLACK" ) ) . toBe ( false ) ;
52
+ } ) ;
53
+ } ) ;
Original file line number Diff line number Diff line change 33
33
"watch" : " yarn run clean && tsc --watch"
34
34
},
35
35
"devDependencies" : {
36
- "@types/jest" : " ^16.0.4 " ,
36
+ "@types/jest" : " ^19.2.2 " ,
37
37
"jest" : " ^18.1.0" ,
38
38
"ts-jest" : " ^18.0.1" ,
39
39
"tslint" : " ^4.3.1" ,
Original file line number Diff line number Diff line change @@ -33,4 +33,10 @@ export namespace Enum {
33
33
}
34
34
return result ;
35
35
}
36
+
37
+ export function isType <
38
+ T extends { [ _ : string ] : any }
39
+ > ( e : T , value : string ) : value is Enum < T > {
40
+ return values ( e ) . indexOf ( value ) !== - 1 ;
41
+ }
36
42
}
Original file line number Diff line number Diff line change 2
2
# yarn lockfile v1
3
3
4
4
5
- " @types/jest@^16.0.4 " :
6
- version "16.0.4 "
7
- resolved "https://registry.yarnpkg.com/@types/jest/-/jest-16.0.4 .tgz#31bdd13e2bdfa71498b022494ba04d407f9f8230 "
5
+ " @types/jest@^19.2.2 " :
6
+ version "19.2.2 "
7
+ resolved "https://registry.yarnpkg.com/@types/jest/-/jest-19.2.2 .tgz#71f428be2fa6eb9f15bb0abc3cade67905f94839 "
8
8
9
9
abab@^1.0.0 :
10
10
version "1.0.3"
You can’t perform that action at this time.
0 commit comments