Skip to content

Generated "const enum OrderDirection" clashes with isolatedModules #316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
iainmerrick opened this issue Jul 1, 2022 · 10 comments
Closed

Comments

@iainmerrick
Copy link

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:

Can this be fixed by changing the exported declaration? I think this might work:

export type OrderDirection = "asc" | "desc"
@aexol
Copy link
Collaborator

aexol commented Jul 1, 2022

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

@aexol
Copy link
Collaborator

aexol commented Jul 1, 2022

A lot of people asked for const enums before instead of enums

@iainmerrick
Copy link
Author

How about avoiding enums entirely and just making it a union of strings?

@aexol
Copy link
Collaborator

aexol commented Jul 1, 2022

ok I'll try and test that with different projects and if it won't cause problems I will change it

@ValentinH
Copy link

ValentinH commented Sep 7, 2022

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:
image

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)

@dt-brahim-bouaboud
Copy link

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.

@aexol
Copy link
Collaborator

aexol commented Apr 24, 2024

This week I will upload new zeus with config and option to generate different enums. Sorry for delay

@klonwar
Copy link

klonwar commented May 29, 2024

config and option to generate different enums

Is this option available in 5.4.2? Any CLI example?

Faced this issue with NextJS and separate zeus client library. NextJS forces usage of isolatedModules and enables it automatically in tsconfig.json during the build. So I'm not able to use zeus enums without @ts-ignore :c

UPD:
Better workaround is to cast string to enum type:

property: 'VALUE' as SomeEnum

@mountainmort
Copy link

Also running into this when trying to use the enums generated by Zeus in my NestJS API. The const enums that are generated can't be used as an enum type on Nest objects so I'm having to re-define the enum and map between the two. Would be great to see this compile option added.

@aexol
Copy link
Collaborator

aexol commented Nov 1, 2024

enums done - now standard are normal enums and if you want constEnums you need a flag

@aexol aexol closed this as completed Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants