zod icon indicating copy to clipboard operation
zod copied to clipboard

The returned issue contains a field that is not described in errors.d.ts

Open historydev opened this issue 4 weeks ago • 3 comments

https://github.com/colinhacks/zod/blob/main/packages/zod/src/v4/core/errors.ts#L117

/* other */
export interface $ZodIssueStringStartsWith extends $ZodIssueInvalidStringFormat {
    format: "starts_with";
    prefix: string;
}
/* other */

When I get an invalid_format error with the starts_with format, I get the following object, but TypeScript throws an error because the type doesn't specify anything about the origin field, similar to the regex format:

{
  origin: "string",
  code: "invalid_format",
  format: "starts_with",
  prefix: "+1",
  path: [ "phone" ],
  message: "Invalid string: must start with \"+1\"",
}

issue.origin // TS2339: Property origin does not exist on type $ZodIssueInvalidStringFormat

Checked zod versions: 4.1.12, 4.1.13

historydev avatar Dec 08 '25 16:12 historydev

Hi @historydev! I'm Dosu and I’m helping the zod team.

You're right—at runtime, Zod includes an origin field in error objects for string format checks like starts_with, but the TypeScript interfaces ($ZodIssueInvalidStringFormat, $ZodIssueStringStartsWith) don't declare this property. This mismatch causes the TS2339 error when you access issue.origin in TypeScript. The omission appears to be an oversight, not an intentional design decision, as other issue types (like too_big/too_small) do include origin in their types, and the runtime code for string checks explicitly adds it to the error object (source, source).

Until the types are updated, you can work around this in TypeScript by using a type assertion (e.g., issue as any or by extending the relevant interface in your own code). This is a known inconsistency in Zod v4, and there’s ongoing discussion about how to handle extra fields like origin in error objects (discussion, related issue). If this answers your question, feel free to close the issue!

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

dosubot[bot] avatar Dec 08 '25 16:12 dosubot[bot]

Pullfrog  | Build this ➔pullfrog.com

pullfrog[bot] avatar Dec 09 '25 00:12 pullfrog[bot]

So you're taking the error object and trying to assign to $ZodIssueStringStartsWith? There are several cases where Zod adds additional properties for additional context not described in the issue types, e.g. note. I'm unclear what you're doing to actually see this error (or why you're doing it).

colinhacks avatar Dec 22 '25 06:12 colinhacks

So you're taking the error object and trying to assign to $ZodIssueStringStartsWith? There are several cases where Zod adds additional properties for additional context not described in the issue types, e.g. note. I'm unclear what you're doing to actually see this error (or why you're doing it).

All very simple. origin field contains the field type and i need it.

historydev avatar Dec 23 '25 07:12 historydev