TypeScript Concepts
Runtime validation — checking untrusted values
External data must be checked at runtime before it can safely satisfy a domain contract. A predicate communicates the result of checks to the compiler, but the implementation itself must be correct. The example checks that input is a non-null object containing a string label; it permits extra fields and does not claim an exact schema. Observed output: 'ready'. Local example verified with TypeScript 5.8.3, strict checking, ES2022/CommonJS and Node.js 22.16.0; declarations enabled.
Formal Syntax & Structure
typescript syntaxtypescript
function isLabel(value: unknown): value is Label { /* runtime checks */ }Working Example
Example Snippettypescript
type Label = { label: string };function isLabel(v: unknown): v is Label { return typeof v === "object" && v !== null && "label" in v && typeof v.label === "string";}const inputs: unknown[] = [{ label: "ready" }, { label: 7 }, null];console.log(inputs.filter(isLabel).map(x => x.label).join(","));