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

lazy doesn't compile when used with default #3491

Open
piotrmarkiewicz1 opened this issue May 15, 2024 · 0 comments
Open

lazy doesn't compile when used with default #3491

piotrmarkiewicz1 opened this issue May 15, 2024 · 0 comments

Comments

@piotrmarkiewicz1
Copy link

here is example from tutorial, but with added field with default:

const baseCategorySchema = z.object({
  name: z.string(),
  val: z.number().default(0),
});

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

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

Compiler complains

Type 'ZodObject<extendShape<{ name: ZodString; val: ZodDefault<ZodNumber>; }, { 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 '{ name: string; subcategories: Category[]; val?: number | undefined; }' is not assignable to type 'Category'.
      Type '{ name: string; subcategories: Category[]; val?: number | undefined; }' is not assignable to type '{ name: string; val: number; }'.
        Types of property 'val' are incompatible.
          Type 'number | undefined' is not assignable to type 'number'.
            Type 'undefined' is not assignable to type 'number'.ts(2322)

I believe this is valid case

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