Skip to article content
FrontendXLesson 26 / 40

Assertion functions and asserts

Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.

advanced · about 45 minutes · 3 exercises

  • Predict
  • Implement
  • Debug

What you will be able to do

#
  • Write an assertion function with an asserts return type.
  • Narrow a value for the rest of the scope.
  • Explain why assertion functions must be called on a named value.

Overview

#

asserts

snippet.ts
function assertIsString(value: unknown): asserts value is string {
  if (typeof value !== "string") throw new Error("not a string");
}

After the call, the checker narrows value to string for the rest of the block.

asserts condition

snippet.ts
function assert(condition: unknown, message: string): asserts condition {
  if (!condition) throw new Error(message);
}

Rules

  • The function must be a declaration or a const with an explicit type; it cannot be a plain arrow with only a return annotation.
  • The asserted value must be a named reference, not an expression that could change.
  • An assertion function must not return normally when the assertion fails.

Assertion versus guard

A guard returns a boolean and narrows inside if. An assertion throws and narrows the rest of the scope. Use a guard when the caller can recover, an assertion when a bad value is a programming error.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Implement

    Implement `assertDefined<T>(value: T): asserts value is NonNullable<T>` that throws when the value is null or undefined.

    starter.typescript
    export function assertDefined<T>(value: T): asserts value is NonNullable<T> {
      // your code
    }
    

    Reveal the reference answer
    reference.typescript
    export function assertDefined<T>(value: T): asserts value is NonNullable<T> {
      if (value === null || value === undefined) {
        throw new Error("value is not defined");
      }
    }
    

    Authored expected output: Compiles with no errors. Runtime: assertDefined(1) is fine; assertDefined(null) throws.

    The asserts return type tells the checker the value is defined after the call.

  2. Exercise 2Predict

    After `assertIsString(x)`, what is the type of `x`? ```ts function assertIsString(v: unknown): asserts v is string { if (typeof v !== "string") throw new Error(); } let x: unknown = "a"; assertIsString(x); ```

    Choose the best answer
  3. Exercise 3Debug

    This fails because an assertion function cannot be an arrow with only a return annotation. Fix it. ```ts const assertNumber = (v: unknown): asserts v is number => { if (typeof v !== "number") throw new Error(); }; ```

    starter.typescript
    const assertNumber = (v: unknown): asserts v is number => {
      if (typeof v !== "number") throw new Error();
    };
    

    Reveal the reference answer
    reference.typescript
    function assertNumber(v: unknown): asserts v is number {
      if (typeof v !== "number") throw new Error();
    }
    

    Authored expected output: Compile error resolved: assertion functions must use a function declaration or a const with an explicit type annotation.

    A function declaration carries the asserts signature directly.

Common misconceptions

#
  • An assertion function validates and returns a value.
  • asserts narrows any expression.
  • Arrow functions can always be assertion functions.

Follow-up practice

#
  • typescript-const-type-params

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.