Skip to content

Commit

Permalink
Merge pull request #12 from dphilipson/docs/keys-values-docs
Browse files Browse the repository at this point in the history
Docs/keys values docs
  • Loading branch information
dphilipson authored Mar 23, 2017
2 parents 8346ea2 + bc329f8 commit 86ff924
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ export type Status = Enum<typeof Status>;
console.log(Status.RUNNING); // -> "running"
```

Two helper functions are provided: `Enum.keys()` and `Enum.values()`, which resemble `Object.keys()`
and `Object.values()` but provide strict typing in their return type:

``` javascript
const FileType = Enum({
PDF: "application/pdf",
Text: "text/plain",
JPEG: "image/jpeg",
});
type FileType = Enum<typeof FileType>;

const keys = Enum.keys(FileType);
// Inferred type: ("PDF" | "Text" | "JPEG")[]
// Return value: ["PDF", "Text", "JPEG"] (not necessarily in that order)

const values = Enum.values(FileType);
// Inferred type: ("application/pdf" | "text/plain" | "image/jpeg")[]
// Return value: ["application/pdf", "text/plain", "image/jpeg"] (not necessarily in that order)
```

## Motivation

Enums are useful for cleanly specifying a type that can take one of a few specific values.
Expand Down Expand Up @@ -127,7 +147,8 @@ I might try to solve both problems by introducing constants for the string liter
issues as well:

``` javascript
type Status = "RUNNING" | "STOPPED";
// Typo on "STOPPED" not caught by anything below without additional boilerplate.
type Status = "RUNNING" | "STPOPED";

// Naive attempts to define constants for these don't work.
const StatusNaive = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript-string-enums",
"version": "0.1.1",
"version": "0.2.0",
"description": "Typesafe string enums in TypeScript.",
"main": "dist/index.js",
"types": "dist/index",
Expand Down

0 comments on commit 86ff924

Please sign in to comment.