zod icon indicating copy to clipboard operation
zod copied to clipboard

Cannot extend schema if target shape has .readonly()

Open vitali-ausianik opened this issue 2 years ago • 2 comments

Zod version: 3.22.4 Typescript version: 5.2.2

Fails with Property  shape  does not exist on type error on last string in example

import { z } from 'zod';

export const Entity = z.object({
  id: z.string(),
}).readonly();

export const IdNameEntity = z
  .object({
    name: z.string(),
  })
  .extend(Entity.shape);

vitali-ausianik avatar Oct 06 '23 09:10 vitali-ausianik

I ran into this as well. I put together a solution and have submitted as PR #3533.

llowrey avatar May 29 '24 23:05 llowrey

Hi, @vitali-ausianik. I'm Dosu, and I'm helping the Zod team manage their backlog. I'm marking this issue as stale.

Issue Summary:

  • You reported a problem with extending a schema in Zod when the target shape uses the .readonly() method, resulting in a TypeScript error.
  • User @llowrey encountered the same issue and proposed a solution.
  • A pull request (PR #3533) was submitted by @llowrey to address the problem.

Next Steps:

  • Please let me know if this issue is still relevant to the latest version of the Zod repository. If so, you can keep the discussion open by commenting on the issue.
  • Otherwise, the issue will be automatically closed in 7 days.

Thank you for your understanding and contribution!

dosubot[bot] avatar Jun 18 '25 16:06 dosubot[bot]

You'd need to access the inner object schema, not the outer ZodReadonly (which has no .shape property)

In the latest Zod 4 versions:

Entity.unwrap().shape

colinhacks avatar Jul 24 '25 18:07 colinhacks