CodeBlanks Logo Icon CodeBlanks

Summary - Assertion functions

Write a safe request handler with assertion functions.

Summary - Assertion functions

Assertion functions form a safe validation layer in a request handler. asserts x is defined checks for null, asserts x is T checks the type. Multiple assertion calls guarantee the request meets all requirements; the subsequent code runs safely.

function assertDefined(x: unknown): asserts x is defined {
  if (x === null || x === undefined) throw new Error("missing");
}

function assertString(x: unknown): asserts x is string {
  if (typeof x !== "string") throw new Error("not string");
}

function handle(input: unknown): string {
  assertDefined(input);
  assertString(input);
  return input.toUpperCase();
}

Practice Write a request handler

Define an assertDefined(x: unknown): asserts x is defined function (with throw new Error("missing") when x === null || x === undefined). Define an assertString(x: unknown): asserts x is string function (with throw new Error("not string") when typeof x !== "string"). Write a handle(input: unknown): string function: call assertDefined(input), call assertString(input), then return input.toUpperCase(). Print the result via console.log(handle("hello")).

Starter code

index.ts
// assertDefined fonksiyonunu tanımla (asserts x is defined, throw ile)

// assertString fonksiyonunu tanımla (asserts x is string, throw ile)

// handle(input: unknown): string fonksiyonunu iki assertion ile yaz

// handle("hello") sonucunu yazdır

Sample solution

index.ts
function assertDefined(x: unknown): asserts x is defined {
  if (x === null || x === undefined) {
    throw new Error("missing");
  }
}

function assertString(x: unknown): asserts x is string {
  if (typeof x !== "string") {
    throw new Error("not string");
  }
}

function handle(input: unknown): string {
  assertDefined(input);
  assertString(input);
  return input.toUpperCase();
}

console.log(handle("hello"));

Expected output

HELLO

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Why are multiple assertion calls made in a request handler?

  • A single assertion is enough
  • A separate assertion per validation step keeps the code safe and organized Correct answer
  • Assertions are skipped at runtime
  • It increases runtime speed

Explanation: Making a separate assertion call for each validation step (null check, type check, etc.) modularizes the validation; each step has a single responsibility and the code stays safe.

2 Assertion chain in handle function

Question
function handle(input: unknown): string {
  assertDefined(input);
  ____
  return input.toUpperCase();
}
Answer
function handle(input: unknown): string {
  assertDefined(input);
  assertString(input);
  return input.toUpperCase();
}

Explanation: After the null check with assertDefined, assertString(input) performs the type check; on the next line input is safely used as string.

3 Assertion functions summary matching

Term Meaning
asserts x is defined narrows out null and undefined
asserts x is T narrows to T
throw yapma when condition fails
Assertion zinciri multiple validations

All lessons in Advanced