graphql-zeus
graphql-zeus copied to clipboard
Generated "const enum OrderDirection" clashes with isolatedModules
I'm using Zeus to generate bindings in one NPM package, and attempting to import and use them in another package.
I'm hitting type errors around this generated code:
export declare const enum OrderDirection {
asc = "asc",
desc = "desc"
}
The problem is that many clients configure TS with isolatedModules: true
, which leads to a type error on import:
error TS2748: Cannot access ambient const enums when the '--isolatedModules' flag is provided.
Using "asc"
and "desc"
directly in the client code doesn't work either, for reasons I can't quite fathom:
Type '"asc"' is not assignable to type 'OrderDirection | null | undefined'.
I don't fully understand why TypeScript can't be configured to handle this, but it seems like exporting const enums is generally considered bad practice:
- Here's a good writeup: https://ncjamieson.com/dont-export-const-enums/
- Unresolved discussion among the TS devs: https://github.com/microsoft/TypeScript/issues/40499
Can this be fixed by changing the exported declaration? I think this might work:
export type OrderDirection = "asc" | "desc"
I can add some zeus config in the future. Exporting enums is considered bad practice, const enums are much lighter but get hidden during transpilation
A lot of people asked for const enums before instead of enums
How about avoiding enums entirely and just making it a union of strings?
ok I'll try and test that with different projects and if it won't cause problems I will change it
For the reasons explained by @iainmerrick and @aexol we were actually not able to use either enum
or const enum
on our project.
To avoid this, we patched this part and indeed used string unions. In the following screenshot, you can see our output on the left and the original Zeus output on the right:
We've been using this on our codebase for a few months and are happy with it.
If this is something that looks good, I'm ready to do a PR to add this option using a flag. 🙂
It it can be useful for someone else, here's the commit on our fork to apply this change: elba-security/graphql-zeus@7b2c6cb
(#6)
Hello @aexol
Any news regarding this issue ? It would be nice to have an option during generation to satisfy everyone
enum
| const enum
| union
Thank you.
This week I will upload new zeus with config and option to generate different enums. Sorry for delay