Sub topic 13
Coercing Strings into Numbers and Dates
Assume you know basic TypeScript schemas, parse, and safeParse. This chapter focuses on a common boundary problem: external data often represents every value as a string, even when your program needs numbers or dates.
Why coercion is needed
A coercion schema converts an input value before applying the rest of the schema's checks. For example, z.coerce.number() turns the string "8080" into the number 8080.
This is useful for:
- Environment variables, which are always strings.
- URL query parameters, which are strings.
- HTML form fields, which usually produce strings.
- JSON request fields that should contain numeric or date values but arrive in an inconsistent format.
Coercion does not replace validation. Add constraints such as int, min, max, or email after conversion.
import { z } from "zod";
const Settings = z.object({
PORT: z.coerce.number().int().min(1).max(65535),
RELEASED_AT: z.coerce.date(),
});
const result = Settings.safeParse({
PORT: "8080",
RELEASED_AT: "2026-11-25T15:34:34.541Z",
});
if (result.success) {
console.log(result.data.PORT); // 8080, a number
console.log(result.data.RELEASED_AT instanceof Date); // true
} else {
console.error(result.error.issues);
}The input contains strings, but successful parsing produces a number and a Date object. A Date represents a point in time. Use an ISO 8601 string with a time zone when the exact instant matters.
Number coercion pitfalls
JavaScript's number conversion has surprising cases. For example, Number("") is 0, and Number(" ") is also 0. Therefore, this schema may accept an empty string:
const Port = z.coerce.number().int().min(1);The minimum of 1 rejects the resulting zero, but a field with a minimum of zero would accept it. Validate that a string is present before coercing it when empty input is invalid.
import { z } from "zod";
const RequiredCount = z
.string()
.trim()
.min(1, "A count is required")
.pipe(z.coerce.number().int().nonnegative());
for (const value of ["12", "", "4.5"]) {
const result = RequiredCount.safeParse(value);
console.log(value, result.success);
}pipe runs the second schema only after the first schema succeeds. This separates string checks from number checks.
| Schema | Input example | Successful output |
|---|---|---|
| `z.coerce.number()` | `"42"` | `42` |
| `z.coerce.date()` | `"2026-11-25T00:00:00Z"` | `Date` object |
| `z.coerce.boolean()` | `"false"` | `true` in JavaScript |
The last row is a warning: JavaScript treats any non-empty string as truthy, so z.coerce.boolean() does not interpret the text "false" as boolean false. For environment flags, explicitly accept "true" and "false" and then transform the selected value.
How boundary conversion works
Use coercion only at a boundary where the incoming representation is known. Inside the application, keep values typed. Repeatedly coercing already parsed data can hide an upstream error.
Exercise
What schema accepts a required string containing a positive integer and returns a number?
Show answer
z.string().trim().min(1).pipe(z.coerce.number().int().positive()).