When parsing inputs that may be invalid (e.g., string → boolean), do not rely on implicit/unclear failure behavior. Instead, decide explicitly how parse errors should be handled and make it deterministic.
Apply this by:
.catch(...)) so invalid values consistently map to a chosen boolean.z.string().boolean() uses logical string values (like “true”/”false”), which may differ from TypeScript/JS Boolean() behavior—use z.coerce.boolean() when you want coercion semantics.Example (explicit error recovery to a boolean):
import { z } from "zod";
// Logical boolean parsing with deterministic fallback on error
const b = z.string()
.boolean()
.catch(() => false); // whenever parsing fails
b.parse("true"); // true
b.parse("False"); // false
b.parse("other"); // false (fallback)
If you expect JS-like coercion semantics instead of strict logical strings, prefer:
const b = z.coerce.boolean();
Enter the URL of a public GitHub repository