Code QuizAdvanced

Assertion Function Uses Wrong Signature

A runtime assertion throws on null but uses a predicate signature instead of asserts.

Codetypescript
function assertIsDefined<T>(value: T): value is NonNullable<T> {
  if (value === undefined || value === null) {
    throw new Error("Not defined");
  }
}

function use(x: string | null) {
  assertIsDefined(x);
  console.log(x.toUpperCase());
}

What is wrong with `assertIsDefined`'s signature?

Watch the code walkthrough

Watch on YouTube →