Skip to content

Commit 075970d

Browse files
rburgstcolinhacks
andauthored
docs: add coercion note to fix compile errors (#4940)
* docs: add coercion note to fix compile errors see #4938 * Tweaks --------- Co-authored-by: Colin McDonnell <[email protected]>
1 parent d257340 commit 075970d

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

packages/docs/content/api.mdx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ z.coerce.bigint(); // BigInt(input)
3737

3838
The coerced variant of these schemas attempts to convert the input value to the appropriate type.
3939

40+
4041
```ts
4142
const schema = z.coerce.string();
4243

@@ -46,6 +47,16 @@ schema.parse(true); // => "true"
4647
schema.parse(null); // => "null"
4748
```
4849

50+
The input type of these coerced schemas is `unknown` by default. To specify a more specific input type, pass a generic parameter:
51+
52+
```ts
53+
const A = z.coerce.number();
54+
type AInput = z.infer<typeof A>; // => number
55+
56+
const B = z.coerce.number<number>();
57+
type BInput = z.infer<typeof B>; // => number
58+
```
59+
4960
<Accordions type="single"><Accordion title="How coercion works in Zod">
5061

5162
Zod coerces all inputs using the built-in constructors.

0 commit comments

Comments
 (0)