You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm encountering a TypeScript error when trying to extend/narrow an enum type in a generic schema context. While TypeScript normally allows narrowing of enum types, this doesn't seem to work properly with Zod's type system.
import{z}from'zod';// Base schema with broader enumconstbaseSchema=z.object({gender: z.enum(["Rüde","Hündin","männlich","weiblich"]).nullable(),// ... other fields});// Trying to create a more specific schema with narrower enumconstdogSchema=z.object({gender: z.enum(["Rüde","Hündin"]).nullable(),// ... other fields});// Generic interface that should accept both schemasinterfaceMyInterface<TextendstypeofbaseSchema>{schema: T;// ... other properties}// This fails with type error even though dogSchema's enum is a subset of baseSchema's enumconstimplementation: MyInterface<typeofdogSchema>={schema: dogSchema};
the type error probobly happens since zod is saying that
Type 'ZodEnum<["Rüde", "Hündin"]>' is not assignable to type 'ZodEnum<["Rüde", "Hündin", "männlich", "weiblich"]>
Expected Behavior
Since the enum in dogSchema is a subset of the enum in baseSchema, and TypeScript normally allows this kind of type narrowing, the implementation should type-check successfully.
Actual Behavior
TypeScript produces an error indicating that the narrower enum type is not assignable to the broader enum type when used in this generic context with Zod schemas.
Possible Solutions
One potential solution might be to add a utility type or method to Zod that explicitly handles enum narrowing in generic contexts, similar to how TypeScript handles normal enum type narrowing.
The text was updated successfully, but these errors were encountered:
I'm encountering a TypeScript error when trying to extend/narrow an enum type in a generic schema context. While TypeScript normally allows narrowing of enum types, this doesn't seem to work properly with Zod's type system.
the type error probobly happens since zod is saying that
Type 'ZodEnum<["Rüde", "Hündin"]>' is not assignable to type 'ZodEnum<["Rüde", "Hündin", "männlich", "weiblich"]>
Expected Behavior
Since the enum in
dogSchema
is a subset of the enum inbaseSchema
, and TypeScript normally allows this kind of type narrowing, the implementation should type-check successfully.Actual Behavior
TypeScript produces an error indicating that the narrower enum type is not assignable to the broader enum type when used in this generic context with Zod schemas.
Possible Solutions
One potential solution might be to add a utility type or method to Zod that explicitly handles enum narrowing in generic contexts, similar to how TypeScript handles normal enum type narrowing.
The text was updated successfully, but these errors were encountered: