Repository navigation
Switch Case not working with String conditions #10802
Description
Activity
In JavaScript (and hence TypeScript),
caseclauses 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 yourswitchstatement will only work for "xml", "gif", "m4a", "dat", "ico" and "resx" and everything else will fall through to thedefaultcase.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; }
Also, you shouldn't use
Stringas a type (I think the team is going to try to error on that in the future) as that refers to theStringglobal object. You should usestringinstead:let extension: string = "bin";
Also, Gitter, StackOverflow or IRC are better places to ask these sort of general questions.
- addedWorking as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug
on Sep 9, 2016 RyanCavanaugh commented
on Sep 9, 2016 MemberMore actionsAddressed by comments above.
Ah thanks for letting me know
RyanCavanaugh commented
on Sep 9, 2016 MemberMore actionsThinking about just banning this outright. It's hard to believe anyone would ever do this on purpose
RyanCavanaugh commented
on Sep 14, 2016 MemberMore actionsThis will now be an error
- addedFixedA PR has been merged for this issueA PR has been merged for this issueand removedWorking as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug
on Sep 14, 2016 - addedSuggestionAn idea for TypeScriptAn idea for TypeScript
on Sep 14, 2016 RyanCavanaugh commented
on Sep 14, 2016 MemberMore actions(Tracking at #10814)
- locked and limited conversation to collaborators
on Jun 19, 2018
TypeScript Version: 1.8.34
Code
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