Skip to content

Commit 6d13e0a

Browse files
committed
Minor improvements to README
1 parent dce526c commit 6d13e0a

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

README.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,9 @@ specified on individual values. For example:
7979
``` javascript
8080
export const Status = Enum({
8181
/**
82-
* Everything is fine.
83-
*
8482
* Hovering over Status.RUNNING in an IDE will show this comment.
8583
*/
8684
RUNNING: "running",
87-
88-
/**
89-
* All is lost.
90-
*/
9185
STOPPED: "stopped",
9286
});
9387
export type Status = Enum<typeof Status>;
@@ -100,9 +94,9 @@ Several helper functions are provided. First are `Enum.keys()` and `Enum.values(
10094

10195
``` javascript
10296
const FileType = Enum({
103-
PDF: "application/pdf",
104-
Text: "text/plain",
105-
JPEG: "image/jpeg",
97+
PDF: "application/pdf",
98+
Text: "text/plain",
99+
JPEG: "image/jpeg",
106100
});
107101
type FileType = Enum<typeof FileType>;
108102

@@ -119,14 +113,17 @@ Also available is `Enum.isType()`, which checks if a value is of a given enum ty
119113
as a type guard.
120114

121115
``` javascript
122-
const Color = Enum("RED", "GREEN", "BLUE", "PUCE");
116+
const Color = Enum("BLACK", "WHITE");
123117
type Color = Enum<typeof Color>;
124118

125119
let selectedColor: Color;
126-
127120
const colorString = getUserInputString(); // Unsanitized string.
121+
122+
// TypeScript error: Type 'string' is not assignable to type '"BLACK" | "WHITE"'.
123+
selectedColor = colorString;
124+
128125
if (Enum.isType(Color, colorString)) {
129-
// Allowed because within type guard.
126+
// No error because within type guard.
130127
selectedColor = colorString;
131128
} else {
132129
throw new Error(`${colorString} is not a valid color`);
@@ -168,11 +165,12 @@ type TriathlonStage = "SWIMMING" | "CYCLING" | "RUNNING";
168165
Then if at a later stage I want to change `Status` to be `"STARTED" | "STOPPED"`, there's no easy
169166
way to do it. I can't globally find/replace `"RUNNING"` to `"STARTED"` because it will also change
170167
the unrelated string constants representing `TriathlonStage`. Instead, I have to examine every
171-
occurrance of the string `"RUNNING"` to see if it needs to change.
168+
occurrance of the string `"RUNNING"` to see if it needs to change. Besides, these kinds of global
169+
non-semantic substitutions should make you nervous.
172170

173171
Another disadvantage of string literals comes when using IDE autocomplete features. It's convenient
174172
to be able to type `Status.` and have autocomplete suggest `Status.RUNNING` and `Status.STOPPED`,
175-
but with string literals no such suggestion appears with current IDEs.
173+
but with string literals no such suggestion is possible.
176174

177175
I might try to solve both problems by introducing constants for the string literals, but this has
178176
issues as well:
@@ -181,7 +179,7 @@ issues as well:
181179
// Typo on "STOPPED" not caught by anything below without additional boilerplate.
182180
type Status = "RUNNING" | "STPOPED";
183181

184-
// Naive attempts to define constants for these don't work.
182+
// Naive attempts to define constants for these don't work, as shown below.
185183
const StatusNaive = {
186184
RUNNING: "RUNNING",
187185
STOPPED: "STOPPED",
@@ -191,7 +189,8 @@ const StatusNaive = {
191189
// string which is not assignable to Status.
192190
const status: Status = StatusNaive.RUNNING;
193191

194-
// Correctly defining constants is annoyingly repetitive.
192+
// Correctly defining constants is annoyingly repetitive. I shouldn't need to
193+
// write each value three times.
195194
const Status = {
196195
RUNNING: "RUNNING" as "RUNNING",
197196
STOPPED: "STOPPED" as "STOPPED",

0 commit comments

Comments
 (0)