zod
zod copied to clipboard
Cannot extend schema if target shape has .readonly()
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);
I ran into this as well. I put together a solution and have submitted as PR #3533.
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!
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