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

Type inference issue with extending/narrowing enum types in generic schemas #3825

Open
AnzeKop opened this issue Oct 31, 2024 · 1 comment
Open

Comments

@AnzeKop
Copy link

AnzeKop commented Oct 31, 2024

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 enum
const baseSchema = z.object({
gender: z.enum(["Rüde", "Hündin", "männlich", "weiblich"]).nullable(),
// ... other fields
});
// Trying to create a more specific schema with narrower enum
const dogSchema = z.object({
gender: z.enum(["Rüde", "Hündin"]).nullable(),
// ... other fields
});
// Generic interface that should accept both schemas
interface MyInterface<T extends typeof baseSchema> {
schema: T;
// ... other properties
}
// This fails with type error even though dogSchema's enum is a subset of baseSchema's enum
const implementation: MyInterface<typeof dogSchema> = {
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.

@m10rten
Copy link
Contributor

m10rten commented Nov 3, 2024

Hi, zod does not actually know that the values are the same, does it?

Have you tried with .extend or .merge?

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

2 participants