Boundary validation
Validating inputs at API, configuration, or boundary layers before typed code consumes them.
TypeScript · Function
Assertion functions combine runtime validation with compile-time narrowing. If the function returns successfully, TypeScript treats an asserted condition or type predicate as established in the rest of the relevant control-flow scope.
An assertion function communicates a runtime invariant through its return type. A signature using asserts condition tells TypeScript the condition is true after successful return, while asserts value is Type narrows a named parameter or property to the specified type; the implementation must enforce that claim, usually by throwing when validation fails.
function assert(condition: unknown, msg?: string): asserts condition;
function assertIsString(value: unknown): asserts value is string;| Name | Type | Description |
|---|---|---|
value / condition | unknown or a declared parameter type | The condition, value, or property whose runtime invariant the function checks. |
msg | string | undefined | An optional diagnostic supplied by the implementation when the assertion fails. |
An assertion signature: either asserts condition or asserts parameterName is Type; the function normally throws when the asserted condition is false.
asserts value signature tells TypeScript that the value is truthy after the function returns.function assertIsString(value: unknown): asserts value is string {
if (typeof value !== "string") throw new Error("Expected a string");
}
function shout(value: unknown): string {
assertIsString(value);
return value.toUpperCase();
}
console.log(shout("hello"));The assertion throws for non-strings; after it returns, TypeScript narrows value to string.
Validating inputs at API, configuration, or boundary layers before typed code consumes them.
Checking that a nullable or broadly typed value satisfies a required invariant before property access or computation.
Progressively narrowing a discriminated union or numeric literal union through multiple successful checks.
asserts condition return type communicate?asserts condition means that, if the function returns normally, TypeScript treats the supplied condition as true for the remaining scope. The function generally throws when the condition is false.
Follow-up: How does asserts value is string differ? It narrows a named value to string after successful return, rather than asserting an arbitrary boolean condition.
An assertion signature can narrow a value based on a type predicate, such as asserts value is string. After the call, control-flow analysis uses that predicate to refine the value’s type.
Follow-up: Can it narrow a union repeatedly? Yes; successive assertion functions can reduce the remaining possible members of a union.
Assertion functions are related to type guards, but they affect control flow after the function continues rather than producing a boolean result used directly in a conditional. Their runtime implementation must establish the promised invariant, commonly by throwing on failure.
Follow-up: What happens if the implementation’s check is wrong? TypeScript can still apply the declared narrowing, so incorrect assertions can make later code unsound at runtime.
Type GuardsNarrowingControl flow analysisType predicates