Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch Case not working with String conditions #10802

Closed
06needhamt opened this issue Sep 9, 2016 · 7 comments
Closed

Switch Case not working with String conditions #10802

06needhamt opened this issue Sep 9, 2016 · 7 comments
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@06needhamt
Copy link

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

@yortus
Copy link
Contributor

yortus commented Sep 9, 2016

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;
    }

@kitsonk
Copy link
Contributor

kitsonk commented Sep 9, 2016

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.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Sep 9, 2016
@RyanCavanaugh
Copy link
Member

Addressed by comments above.

@06needhamt
Copy link
Author

Ah thanks for letting me know

@RyanCavanaugh
Copy link
Member

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

@RyanCavanaugh
Copy link
Member

This will now be an error

@RyanCavanaugh RyanCavanaugh added Fixed A PR has been merged for this issue and removed Working as Intended The behavior described is the intended behavior; this is not a bug labels Sep 14, 2016
@mhegazy mhegazy added this to the TypeScript 2.1 milestone Sep 14, 2016
@mhegazy mhegazy added the Suggestion An idea for TypeScript label Sep 14, 2016
@RyanCavanaugh
Copy link
Member

(Tracking at #10814)

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants