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

Recursive "Types of property are incompatible." error when using transform() or catch() #3890

Open
krokyze opened this issue Dec 6, 2024 · 0 comments

Comments

@krokyze
Copy link

krokyze commented Dec 6, 2024

Hey, I'm trying to define my schemas, everything was fine until I needed to add recursion. I'm having issues with both the date and enum fields. Here's and example code:

const Color = {
	RED: 'red',
	GREEN: 'green',
	UNKNOWN: 'unknown',
} as const;

const ColorEnum = z.nativeEnum(Color);
type ColorEnum = z.infer<typeof ColorEnum>;

const baseCategorySchema = z.object({
	name: z.string(),
	date: z.number().transform((v) => new Date(v * 1000)),
	enum: ColorEnum.catch(Color.UNKNOWN),
});

type Category = z.infer<typeof baseCategorySchema> & {
	subcategories: Category[];
};

const categorySchema: z.ZodType<Category> = baseCategorySchema.extend({
	subcategories: z.lazy(() => categorySchema.array()),
});

But when I compile I get error, if I remove transform() then it works.

TS2322: Type 'ZodObject<extendShape<{ name: ZodString; date: ZodEffects<ZodNumber, Date, number>; }, { subcategories: ZodLazy<ZodArray<ZodType<Category, ZodTypeDef, Category>, "many">>; }>, "strip", ZodTypeAny, { ...; }, { ...; }>' is not assignable to type 'ZodType<Category, ZodTypeDef, Category>'.
  Types of property '_input' are incompatible.
    Type '{ date: number; name: string; subcategories: Category[]; }' is not assignable to type 'Category'.
      Type '{ date: number; name: string; subcategories: Category[]; }' is not assignable to type '{ date: Date; name: string; }'.
        Types of property 'date' are incompatible.
          Type 'number' is not assignable to type 'Date'.

TS2322: Type 'ZodObject<extendShape<{ name: ZodString; enum: ZodCatch<ZodNativeEnum<{ readonly RED: "red"; readonly GREEN: "green"; readonly UNKNOWN: "unknown"; }>>; }, { subcategories: ZodLazy<...>; }>, "strip", ZodTypeAny, { ...; }, { ...; }>' is not assignable to type 'ZodType<Category, ZodTypeDef, Category>'.
  Types of property '_input' are incompatible.
    Type '{ name: string; subcategories: Category[]; enum?: unknown; }' is not assignable to type 'Category'.
      Type '{ name: string; subcategories: Category[]; enum?: unknown; }' is not assignable to type '{ name: string; enum: "unknown" | "red" | "green"; }'.
        Types of property 'enum' are incompatible.
          Type 'unknown' is not assignable to type '"unknown" | "red" | "green"'.
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