6 ms·
Really? This isn't even close to valid JS. enum Color { red = 1, green = 2, blue = 4 } namespace Color { export function mixColor(colorName: s
by russellbeattie 8y ago
Really? This isn't even close to valid JS.
enum Color {
red = 1,
green = 2,
blue = 4
}
namespace Color {
export function mixColor(colorName: string) {
if (colorName == "yellow") {
return Color.red + Color.green;
}
else if (colorName == "white") {
return Color.red + Color.green + Color.blue;
}
else if (colorName == "magenta") {
return Color.red + Color.blue;
}
else if (colorName == "cyan") {
return Color.green + Color.blue;
}
}
}
* Edited with a better example.
- realusername 8y agoenums are probably the only TS thing which does not exist in plain JS (at least I can't think of any other one), and it's quite easy to replace. In your example you just replace enum by a hashmap and remove all the types and it's a valid JS file, it's not really that different. It would be quite easy for an automated tool to do that.
- glitchdout 8y agoIt's not that simple because you can reverse lookup a TS enum. So: enum Color { Red = 1, Green = 2 } would be the same as: var Color = { Red: 1, 1: 'Red', Green: 2, 2: 'Green' } See for yourself: https://www.typescriptlang.org/play/#src=enum%20Color%7B%0D%0A%20%20%20%20Red%20%3D%201%2C%20Green%20%3D%202%0D%0A%7D%0D%0A%0D%0A%2F%2F%20To%20String%0D%0A%20var%20green%3A%20string%20%3D%20Color%5BColor.Green%5D%3B%0D%0A%0D%0A%2F%2F%20To%20Enum%0D%0Avar%20color%20%3A%20Color%20%3D%20Color%5Bgreen%5D%3B%0D%0A%0D%0A https://www.typescriptlang.org/play/#src=enum%20Color%7B%0D%... But I agree with you, it would be quite easy for an automated tool to replace a TS enum with pure JS.
- realusername 8y agoAh that's a good point, thanks for bringing it. I did know TS enums supported that, I thought it was just some syntax sugar for a hashmap.