zod icon indicating copy to clipboard operation
zod copied to clipboard

Inability to retrieve keys from z.nativeEnum type

Open 3tatsu opened this issue 9 months ago • 0 comments

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.

3tatsu avatar May 07 '24 09:05 3tatsu