zod
zod copied to clipboard
Inability to retrieve keys from z.nativeEnum type
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.