File tree Expand file tree Collapse file tree 1 file changed +5
-16
lines changed
Expand file tree Collapse file tree 1 file changed +5
-16
lines changed Original file line number Diff line number Diff line change @@ -84,22 +84,7 @@ console.log(state);
8484
8585### Why not string literals?
8686
87- Using string literals throughout a program leaves it vulnerable to bugs caused by typos or
88- incomplete refactors. For example:
89-
90- ``` javascript
91- type Status = " RUNNING" | " STOPPED" ;
92-
93- ...
94-
95- // Typo "SOTPPED" is not caught by typechecker.
96- // Pretty bad bug- this block will never run.
97- if (state .status === " SOTPPED" ) {
98- soundTheAlarm ();
99- }
100- ```
101-
102- Further, string literals make refactoring difficult. Suppose I have two enums:
87+ String literals make refactoring difficult. Suppose I have two enums:
10388
10489``` javascript
10590type Status = " RUNNING" | " STOPPED" ;
@@ -111,6 +96,10 @@ way to do it. I can't globally find/replace `"RUNNING"` to `"STARTED"` because i
11196the unrelated string constants representing ` TriathlonStage ` . Instead, I have to examine every
11297occurrance of the string ` "RUNNING" ` to see if it needs to change.
11398
99+ Another disadvantage of string literals comes when using IDE autocomplete features. It's convenient
100+ to be able to type ` Status. ` and have autocomplete suggest ` Status.RUNNING ` and ` Status.STOPPED ` ,
101+ but with string literals no such suggestion appears with current IDEs.
102+
114103I might try to solve both problems by introducing constants for the string literals, but this has
115104issues as well:
116105
You can’t perform that action at this time.
0 commit comments