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

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -