type assertion
Telling TypeScript to treat a value as a certain type with the as keyword, without checking that it really is.
In more detail
A type assertion silences the compiler but does nothing at run time, so data that does not match will still crash later. Prefer parsing untrusted data with a schema, which produces a correctly typed value only when the data really matches.
Example
const user = data as User // no runtime check at allRelated terms
- validation schema — A description of what valid data looks like, checked by code while the program runs so bad data is rejected at the door.
- TypeScript — JavaScript with added type annotations, checked by a compiler before the code runs.
- safeParse — The Zod method that checks data against a schema and returns a success flag with either the parsed data or the errors, instead of throwing.
Taught in
- End-to-End Type Safety: Shared Schemas & DTOs — Production Full-Stack Engineering
- API Contract Architecture: OpenAPI 3.1 & Schema Truth — Production Full-Stack Engineering