Validate external input
A boundary receives unknown data and must reject invalid values before typed code uses it.
TypeScript · Const
asserts defines assertion signatures that narrow types after a function returns successfully. It supports asserting a condition or declaring that a parameter has a specific type.
asserts marks a function as an assertion function. If the function returns normally, TypeScript uses either the asserted condition or a parameter is Type predicate to narrow types in the remaining control-flow scope; the implementation must enforce the claim at runtime.
function assert(condition: any, msg?: string): asserts condition
function assertIsString(val: any): asserts val is stringAn assertion signature narrows the checked condition or named value after the function returns normally.
asserts condition in the return-type position.asserts parameter is Type to narrow a particular value.NonNullable<T> to narrow away both null and undefined.asserts condition, the condition is considered true only after the function returns; an unsound assertion can make later code incorrectly unreachable or over-narrowed.function assertIsString(value: unknown): asserts value is string {
if (typeof value !== "string") {
throw new TypeError("Expected a string");
}
}
function shout(value: unknown) {
assertIsString(value);
return value.toUpperCase();
}
console.log(shout("hello"));The assertion validates the runtime value and lets TypeScript treat it as a string after the call.
A boundary receives unknown data and must reject invalid values before typed code uses it.
A generic value may be absent during lookup but must be present before processing.
An invariant must hold for the rest of a function after a check.
asserts do?An assertion signature tells TypeScript what is guaranteed when a function returns normally. asserts condition narrows the condition itself, while asserts value is Type narrows a named parameter or property.
Follow-up: How does this differ from a type predicate? A type predicate returns a boolean for the caller to branch on; an assertion function generally throws on failure and narrows after the call continues.
asserts condition affect control flow?For asserts condition, the condition passed to the function is treated as true for the rest of the containing scope if the function returns. The typical implementation throws when the condition is false.
Follow-up: What happens if the implementation fails to throw? The compiler still trusts the signature, so invalid states can be accepted and subsequent narrowing can be unsound.
asserts value is Type?asserts value is string states that the named value is a string after successful return. This resembles a type predicate, but it changes control flow through the assertion call instead of returning a boolean that must be tested.
Follow-up: Can the asserted type be generic? Yes; the documentation demonstrates asserts val is NonNullable<T> to remove null and undefined from a generic value.
type predicatestype guardsNonNullablenarrowingassertion signatures