Graphql-code-generator: Generate enum as `const enum` on Typescript

Created on 15 Jan 2020  路  6Comments  路  Source: dotansimha/graphql-code-generator

Is your feature request related to a problem? Please describe.


Now, graphql codegen creates Typescript code for enum as a normal enum. It's better if we use const enum, the compiler can provide more optimization

Describe the solution you'd like

In spite of generating enum, we should return const enum

Describe alternatives you've considered

Additional context

Most helpful comment

It seems like we have an issue with generating the markdown file of the config flags for typescript plugin.
There is a config flags constEnums: true that will generate that for you.

I also fixed (https://github.com/dotansimha/graphql-code-generator/pull/3343) the documentation issues, and it will be available in the website during the next deployment (very soon).
You can find all docs here: https://github.com/dotansimha/graphql-code-generator/blob/797d0f3abead2b95cf099ca544192c49c76af9f3/packages/plugins/typescript/typescript/src/config.ts#L65

All 6 comments

Keep in mind that Babel cannot transpile TypeScript code that uses const enum

ref: https://babeljs.io/docs/en/babel-plugin-transform-typescript#caveats

I met the same issue.
If the behavior is intended, I want to know the reason why.
If not, I'm willing to work on this issue.

@LinusU There is a babel plugin for it https://www.npmjs.com/package/babel-plugin-const-enum

And of course, we can provide an option for toggling const enum and enum

Sure, but note that that plugin just strips the const from the enums, it doesn't actually implement const enums in Babel (since that would require having a knowledge of the type information, which Babel doesn't have).

I just wanted to raise this so that it's considered, e.g. if this is shipped and turn on by default peoples builds could start failing. I therefor think that this shouldn't be shipped unless in a new major version or behind an opt-in flag....

Just my 2垄 鈽猴笍

@LinusU agree. I do think that a flag/option will be the best choice.

It seems like we have an issue with generating the markdown file of the config flags for typescript plugin.
There is a config flags constEnums: true that will generate that for you.

I also fixed (https://github.com/dotansimha/graphql-code-generator/pull/3343) the documentation issues, and it will be available in the website during the next deployment (very soon).
You can find all docs here: https://github.com/dotansimha/graphql-code-generator/blob/797d0f3abead2b95cf099ca544192c49c76af9f3/packages/plugins/typescript/typescript/src/config.ts#L65

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NickClark picture NickClark  路  3Comments

steebchen picture steebchen  路  3Comments

edorivai picture edorivai  路  3Comments

rogerg93 picture rogerg93  路  3Comments

zenVentzi picture zenVentzi  路  3Comments