Capítulo 7 de 61
z.templateLiteral() (introduced in Zod 4) builds a schema from a mix of literal strings and sub-schemas, mirroring TypeScript's template literal types, and validates that the input matches the resulting string pattern.
z.templateLiteral([...parts]): each part is either a literal (string/number/etc.) or a schema whose inferred type is assignable to string | number | bigint | boolean | null | undefined.const schema = z.templateLiteral([ "hello, ", z.string(), "!" ]);
// type: `hello, ${string}!`
z.templateLiteral([ "high", z.literal(5) ]);
// `high5`
z.templateLiteral([ z.nullable(z.literal("grassy")) ]);
// `grassy` | `null`
z.templateLiteral([ z.number(), z.enum(["px", "em", "rem"]) ]);
// `${number}px` | `${number}em` | `${number}rem`
z.templateLiteral() when a string's valid shape is naturally expressed as a template (e.g. CSS values like ${number}px), instead of a hand-written regex.string.