question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

String collisions are not detected when the strings come from enums

See original GitHub issue

TypeScript 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.

Playground Link: https://www.typescriptlang.org/play/#src=enum BuildingActions { sort %3D "sort" } enum AlarmActions { sort %3D "sort" } interface SortBuildingsAction { type%3A BuildingActions.sort%2C buildingCriteria%3A any } interface SortAlarmsAction { type%3A AlarmActions.sort%2C alarmCriteria%3A any } type Action %3D SortAlarmsAction | SortBuildingsAction%3B function reduce(a%3A Action) { switch (a.type) { case BuildingActions.sort%3A console.log(a.buildingCriteria) break%3B case AlarmActions.sort%3A console.log(a.alarmCriteria)%3B break } }

Related Issues: No related issues found.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
RyanCavanaughcommented, Aug 24, 2018

@Kingwl This should just be a specific logic in detecting duplicate case labels. Not sure if that answers your question

0reactions
ajafffcommented, Aug 28, 2018

Until this is implemented/fixed in TypeScript, you can use a linter rule I wrote to detect duplicate case clauses in switch statements: no-duplicate-case For a description on how to use it, see https://github.com/fimbullinter/wotan/blob/master/packages/wotan/README.md

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found