Skip to content
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

Inability to retrieve keys from z.nativeEnum type #3468

Open
AtsumiFlex opened this issue May 7, 2024 · 0 comments
Open

Inability to retrieve keys from z.nativeEnum type #3468

AtsumiFlex opened this issue May 7, 2024 · 0 comments

Comments

@AtsumiFlex
Copy link

Hello,

I am using zod to work with TypeScript enums and have encountered an issue when trying to retrieve the keys from an enum using z.nativeEnum. The z.nativeEnum function does not seem to provide a method to retrieve the keys of the enum, unlike the standard TypeScript keyof type operator.

Here's an example illustrating the issue:

// Definition of SomeEnum using TypeScript
enum SomeEnum {
    PNG = 1,
    APNG = 2,
    Lottie = 3,
    GIF = 4,
};

// Attempt to use keyof on SomeEnum directly
type SomeEnumKeyof = keyof typeof SomeEnum;  // This works as expected

// Using z.nativeEnum with SomeEnum
const SomeEnumSchema = z.nativeEnum(SomeEnum);

// Attempting to use keyof on z.nativeEnum result
// The following line throws an error because .keyof() method does not exist
const keySomeEnum = SomeEnumSchema.keyof(); // This does not work

Could you provide guidance on how to properly retrieve the keys from an enum defined with z.nativeEnum? Is there a workaround or a method that I might be missing?

Thank you for your assistance, and apologies for any confusion caused by my language skills.

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

1 participant