You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to define a Zod schema for a type based on a template literal (e.g., p${string}) and use it in a generateJsonSchema function, but I am running into a TypeScript type error.
Here is the example code I am working with:
importzodToJsonSchemafrom'zod-to-json-schema';typepStr= `p${string}`;functiongenerateJsonSchema<T>(t: z.ZodSchema<T>){returnzodToJsonSchema(t)}generateJsonSchema<pStr>(z.string().refine((v): v is pStr=>v.startsWith('p').describe('project ID')));
This results in the following TypeScript error:
Type 'ZodEffects<ZodString, `p${string}`, string>' is not assignable to type 'ZodType<`p${string}`, ZodTypeDef, `p${string}`>'.
Property '_input' is incompatible.
Type 'string' is not assignable to type '`p${string}`'. ts(2345)
Issue:
I am trying to define a refined Zod schema for a type like pStr (which is a template literal type p${string}). However, TypeScript complains about the incompatibility between string and the template literal type when trying to pass the schema to the generateJsonSchema function.
I would like to know:
Is there a way to work around this issue while still using template literal types in Zod schemas?
Is there a way to generate a JSON schema for a refined type like pStr?
The goal is to provide a type (like pStr) and have Zod output the corresponding JSON schema, either manually or automatically, without running into this TypeScript compatibility error.
Environment:
Zod version: 3.24.1
TypeScript version: 5.0.0
The text was updated successfully, but these errors were encountered:
I am trying to define a Zod schema for a type based on a template literal (e.g.,
p${string}
) and use it in agenerateJsonSchema
function, but I am running into a TypeScript type error.Here is the example code I am working with:
This results in the following TypeScript error:
Issue:
I am trying to define a refined Zod schema for a type like
pStr
(which is a template literal typep${string}
). However, TypeScript complains about the incompatibility betweenstring
and the template literal type when trying to pass the schema to thegenerateJsonSchema
function.I would like to know:
pStr
?The goal is to provide a type (like
pStr
) and have Zod output the corresponding JSON schema, either manually or automatically, without running into this TypeScript compatibility error.Environment:
The text was updated successfully, but these errors were encountered: