Compile an enum in TypeScript -
what enums become @ runtime in typescript?
fruit.ts
enum fruit {apple, orange}; main.ts
var bowl = [fruit.apple, fruit.orange]; console.log(bowl); the generated main.js file stays same .ts, won't work.
the js code be:
// fruit.js var fruit; (function (fruit) { fruit[fruit["apple"] = 0] = "apple"; fruit[fruit["orange"] = 1] = "orange"; })(fruit || (fruit = {})); // main.js var bowl = [fruit.apple, fruit.orange]; console.log(bowl); you're looking const enum though:
const enum fruit {apple, orange}; if use that, values of enum inlined javascript:
// fruit.js: empty.. nothing in here // main.js var bowl = [0 /* apple */, 1 /* orange */]; console.log(bowl); you can use const enum when js code regular enum generates isn't necessary in application.
Comments
Post a Comment