3 ms·
enums 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
by realusername 8y ago
enums 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.