CodeBlanks Logo Icon CodeBlanks

Summary - Type guards

Write a safe data validation layer with type guards.

Summary - Type guards

Type guards can be combined to form a safe data validation layer. A user-defined guard narrows with is; an assertion function narrows by throwing with asserts. Primitive guards check primitive types with typeof. Together they safely process an unknown input.

function isString(x: unknown): x is string {
  return typeof x === "string";
}

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

function process(v: unknown): string {
  if (isString(v)) return `string: ${v}`;
  assertNumber(v);
  return `number: ${v}`;
}

Practice Write a data validation layer

Define an isString(x: unknown): x is string guard function (with typeof x === "string"). Define an assertNumber(x: unknown): asserts x is number function (with throw new Error("not number") when typeof x !== "number"). Write a process(v: unknown): string function: if isString(v) is true, return `string: ${v}`; otherwise call assertNumber(v) and return `number: ${v}`. Print results via console.log(process("hello")) and console.log(process(42)).

Starter code

index.ts
// isString guard fonksiyonunu tanımla (string predicate, typeof ile)

// assertNumber fonksiyonunu tanımla (asserts x is number, throw ile)

// process(v: unknown): string fonksiyonunu guard ve assert ile yaz

// process("hello") ve process(42) sonuçlarını yazdır

Sample solution

index.ts
function isString(x: unknown): x is string {
  return typeof x === "string";
}

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

function process(v: unknown): string {
  if (isString(v)) {
    return `string: ${v}`;
  }
  assertNumber(v);
  return `number: ${v}`;
}

console.log(process("hello"));
console.log(process(42));

Expected output

string: hello
number: 42

Questions and answers

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

1 What is the core difference between a type guard (is) and an assertion function (asserts)?

  • Guards work only with classes
  • A guard returns true/false, an assertion throws or narrows Correct answer
  • Assertions work only with primitive types
  • They are the same

Explanation: A type guard with is returns true/false and narrows in an if; an assertion function with asserts either completes to narrow or throws, with no if needed.

2 assertNumber assertion

Question
function assertNumber(x: unknown): ____ {
  if (typeof x !== "number") throw new Error("not number");
}
Answer
function assertNumber(x: unknown): asserts x is number {
  if (typeof x !== "number") throw new Error("not number");
}

Explanation: The asserts x is number return type tells TypeScript that x is a number when the function completes; if it throws, no narrowing happens.

3 Type guards summary matching

Term Meaning
is (type guard) returns true/false
asserts (assertion) throw or narrow
typeof primitive type check
unknown safe narrowing with guard

All lessons in Advanced