Skip to content

Commit 030643b

Browse files
authored
Merge pull request #19 from dphilipson/feature/is-type-guard
Add isType type guard function
2 parents 0ba53c8 + 34eea37 commit 030643b

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ export type Status = Enum<typeof Status>;
8585
console.log(Status.RUNNING); // -> "running"
8686
```
8787

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:
9090

9191
``` javascript
9292
const FileType = Enum({
@@ -105,6 +105,27 @@ const values = Enum.values(FileType);
105105
// Return value: ["application/pdf", "text/plain", "image/jpeg"] (not necessarily in that order)
106106
```
107107

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+
108129
## Motivation
109130

110131
Enums are useful for cleanly specifying a type that can take one of a few specific values.

__tests__/test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe("Enum.keys", () => {
2525
});
2626
expect(Enum.keys(e)).toEqual(expect.arrayContaining([ "WHITE", "BLACK" ]));
2727
});
28-
};
28+
});
2929

3030
describe("Enum.values", () => {
3131
it("returns the values of an enum object", () => {
@@ -36,3 +36,18 @@ describe("Enum.values", () => {
3636
expect(Enum.values(e)).toEqual(expect.arrayContaining([ "white", "black" ]));
3737
});
3838
});
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+
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"watch": "yarn run clean && tsc --watch"
3434
},
3535
"devDependencies": {
36-
"@types/jest": "^16.0.4",
36+
"@types/jest": "^19.2.2",
3737
"jest": "^18.1.0",
3838
"ts-jest": "^18.0.1",
3939
"tslint": "^4.3.1",

src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,10 @@ export namespace Enum {
3333
}
3434
return result;
3535
}
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+
}
3642
}

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
# yarn lockfile v1
33

44

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"
88

99
abab@^1.0.0:
1010
version "1.0.3"

0 commit comments

Comments
 (0)