Skip to content

Switch Case not working with String conditions  #10802

Description

TypeScript Version: 1.8.34

Code

// A *self-contained* demonstration of the problem follows...
let extension: String = "bin";
        switch (extension) {
            case "txt", "xml":
                return ResourceType.PlainText;
            case "bmp", "jpg", "jpeg", "png", "tiff", "psd", "tga", "gif":
                return ResourceType.Image;
            case "mp3", "wav", "ogg", "flac", "wma", "m4a":
                return ResourceType.Audio;
            case "bin", "dat":
                return ResourceType.Binary;
            case "ico":
                return ResourceType.Icon;
            case "rc", "resx":
                return ResourceType.ResourceScript;
            default:
                return ResourceType.Unknown;
}

Expected behavior:
The Switch case will return the correct value based on the condition ex when extension == "bin" ResourceType.Binary should be returned
Actual behavior:
The Default value is always returned

Activity

  1. yortus commented on Sep 9, 2016

    @yortus
    Contributor

    In JavaScript (and hence TypeScript), case clauses don't take comma-separated lists like that. But it's not an error, because a comma-separated list of expressions is a valid expression that evaluates to the result of the rightmost expression in the list. So your switch statement will only work for "xml", "gif", "m4a", "dat", "ico" and "resx" and everything else will fall through to the default case.

    I think this is what you meant:

        switch (extension) {
            case "txt":
            case "xml":
                return ResourceType.PlainText;
            case "bmp":
            case "jpg":
            case "jpeg":
            case "png":
            case "tiff":
            case "psd":
            case "tga":
            case "gif":
                return ResourceType.Image;
            case "mp3":
            case "wav":
            case "ogg":
            case "flac":
            case "wma":
            case "m4a":
                return ResourceType.Audio;
            case "bin":
            case "dat":
                return ResourceType.Binary;
            case "ico":
                return ResourceType.Icon;
            case "rc":
            case "resx":
                return ResourceType.ResourceScript;
            default:
                return ResourceType.Unknown;
        }
  2. kitsonk commented on Sep 9, 2016

    @kitsonk
    Contributor

    Also, you shouldn't use String as a type (I think the team is going to try to error on that in the future) as that refers to the String global object. You should use string instead:

    let extension: string = "bin";

    Also, Gitter, StackOverflow or IRC are better places to ask these sort of general questions.

  3. RyanCavanaugh commented on Sep 9, 2016

    @RyanCavanaugh
    Member

    Addressed by comments above.

  4. 06needhamt commented on Sep 9, 2016

    @06needhamt
    Author

    Ah thanks for letting me know

  5. RyanCavanaugh commented on Sep 9, 2016

    @RyanCavanaugh
    Member

    Thinking about just banning this outright. It's hard to believe anyone would ever do this on purpose

  6. RyanCavanaugh commented on Sep 14, 2016

    @RyanCavanaugh
    Member

    This will now be an error

  7. added
    FixedA PR has been merged for this issue
    and removed
    Working as IntendedThe behavior described is the intended behavior; this is not a bug
    on Sep 14, 2016
  8. RyanCavanaugh commented on Sep 14, 2016

    @RyanCavanaugh
    Member

    (Tracking at #10814)

  9. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions