TypeScript Syntax and Type Operators
asserts — assertion function signature
An assertion signature describes a condition guaranteed if the function completes normally. The checker narrows the argument after a successful call, rather than using a boolean result. The implementation must actually throw or otherwise stop execution when its claim is false. An incorrectly declared assertion can conceal a runtime defect. A bare asserts condition signature can assert truthiness instead. Example output: "5". Tested in the strict TypeScript 5.8.3 / ES2022 baseline; the example is not a runtime type validator.
Formal Syntax & Structure
typescript syntaxtypescript
function assertText(value: unknown): asserts value is string { if (typeof value !== "string") throw new Error("Expected text"); }Working Example
Example Snippettypescript
function assertText(value: unknown): asserts value is string { if (typeof value !== "string") throw new Error("Expected text");}function size(value: unknown) { assertText(value); return value.length;}console.log(size("ready"));