Explore Library
Code QuizIntermediate

Helper Function Loses Narrowing

Extracting a null check into a boolean-returning helper discards narrowing at the call boundary.

Codetypescript
function isDefined(x: string | undefined) {
  return x !== undefined;
}

function process(name: string | undefined) {
  if (isDefined(name)) {
    console.log(name.toUpperCase());
  }
}

Why does `name.toUpperCase()` fail to compile inside the `if` block?