String collisions are not detected when the strings come from enums
See original GitHub issueTypeScript Version: 3.1.0-dev.20180822
Search Terms: enum string
Code
enum BuildingActions {
sort = "sort"
}
enum AlarmActions {
sort = "sort"
}
interface SortBuildingsAction {
type: BuildingActions.sort,
buildingCriteria: any
}
interface SortAlarmsAction {
type: AlarmActions.sort,
alarmCriteria: any
}
type Action = SortAlarmsAction | SortBuildingsAction;
function reduce(a: Action) {
switch (a.type) {
case BuildingActions.sort:
console.log(a.buildingCriteria)
break;
case AlarmActions.sort:
console.log(a.alarmCriteria);
break
}
}
Expected behavior:
- Type error on lines 22-30: BuildingActions.sort is equals to AlarmActions.sort.
- Dead code detected at lines 26-28.
Actual behavior:
Code compiles to Javascript without any warnings. The case
always enters on the first branch.
Related Issues: No related issues found.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How do I convert a string to enum in TypeScript?
A possible solution is to manually check values and cast the value to the enum. Please notice that it will work only with...
Read more >String Hashes - Dangling Pointers
But strings are no fun: storing them efficiently is a headache, they're bigger and they're slower than enums. Fortunately there's an alternative ...
Read more >Defining an Enum - The Rust Programming Language
This code illustrates that you can put any kind of data inside an enum variant: strings, numeric types, or structs, for example. You...
Read more >[Belay the C++] Best ways to convert an enum to a string - Reddit
I do not understand how byte order comes into play here. ... Everyone asking for the ability to convert enum values to strings...
Read more >Protocol Buffer Basics: Java - Google Developers
This tutorial provides a basic Java programmer's introduction to working with ... string name = 1; optional int32 id = 2; optional string...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@Kingwl This should just be a specific logic in detecting duplicate
case
labels. Not sure if that answers your questionUntil this is implemented/fixed in TypeScript, you can use a linter rule I wrote to detect duplicate
case
clauses inswitch
statements: no-duplicate-case For a description on how to use it, see https://github.com/fimbullinter/wotan/blob/master/packages/wotan/README.md