Closed
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
Metadata
Metadata
Assignees
Type
Projects
Relationships
Development
No branches or pull requests
Activity
yortus commentedon 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 yourswitch
statement will only work for "xml", "gif", "m4a", "dat", "ico" and "resx" and everything else will fall through to thedefault
case.I think this is what you meant:
kitsonk commentedon 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 theString
global object. You should usestring
instead:Also, Gitter, StackOverflow or IRC are better places to ask these sort of general questions.
RyanCavanaugh commentedon Sep 9, 2016
Addressed by comments above.
06needhamt commentedon Sep 9, 2016
Ah thanks for letting me know
RyanCavanaugh commentedon Sep 9, 2016
Thinking about just banning this outright. It's hard to believe anyone would ever do this on purpose
RyanCavanaugh commentedon Sep 14, 2016
This will now be an error
RyanCavanaugh commentedon Sep 14, 2016
(Tracking at #10814)