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
The `z.templateLiteral` API can handle any number of string literals (e.g. `"hello"`) and schemas. Any schema with an inferred type that's assignable to `string | number | bigint | boolean | null | undefined` can be passed.
To make a schema *optional* (that is, to allow `undefined` inputs).
@@ -848,29 +881,6 @@ No value will pass validation.
848
881
z.never(); // inferred type: `never`
849
882
```
850
883
851
-
## Template literals
852
-
853
-
854
-
> **💎 New in Zod 4**
855
-
856
-
Zod 4 finally implements one of the last remaining unrepresented features of TypeScript's type system: template literals. Virtually all primitive schemas can be used in `z.templateLiteral`: strings, string formats like `z.email()`, numbers, booleans, enums, literals (of the non-template variety), optional/nullable, and other template literals.
This API can be used to overwrite existing fields! Be careful with this power! If the two schemas share keys, B will override A.
1023
1033
</Callout>
1024
1034
1025
-
Despite the existence of the `.extend` API, the recommended way to extend an object schema is to create an entirely new schema:
1035
+
Despite the existence of the `.extend` API, the recommended way to extend an object schema is to create an entirely new schema by destructuring the original schema's `shape` property.
1026
1036
1027
1037
```ts
1028
1038
const DogWithBreed =z.object({
@@ -2397,7 +2407,7 @@ type ReadonlyUser = z.infer<typeof ReadonlyUser>;
2397
2407
</Tab>
2398
2408
</Tabs>
2399
2409
2400
-
This returns a new schema that wraps the original. The new schema's inferred type will be marked as `readonly`. Note that this only affects objects, arrays, tuples, `Set`, and `Map` in TypeScript:
2410
+
The inferred type of the new schemas will be marked as `readonly`. Note that in TypeScript, this only affects objects, arrays, tuples, `Set`, and `Map`:
Inputs will be parsed using the original schema, then the result will be frozen with [`Object.freeze()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze) to prevent modifications.
2433
+
Inputs will be parsed like normal, then the result will be frozen with [`Object.freeze()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze) to prevent modifications.
The `z.templateLiteral` API can handle any number of string literals (e.g. `"hello"`) and schemas. Any schema with an inferred type that's assignable to `string | number | bigint | boolean | null | undefined` can be passed.
0 commit comments