'TypeScript - Utility function causing type guard is not being inferred

I'm trying to abstract typeof x === 'undefined'. However, to the extend of my knowledge, TypeScript doesn't seem to recognize _isUndefined() as Type Guard.

resultA seems to still be number | undefined.

Is there something I'm missing?

function _isUndefined<Type>(variable: Type): boolean {
  return typeof variable === 'undefined'
}

function sumButUndefinedIfAIsZero(a: number, b: number) {
  if (a == 0) return
  return a + b
}

function bar(x: number, y: number) {
  return x * y
}

function doSomethingWithisUndefined() {
  const t = 0
  const u = 2

  const resultA = sumButUndefinedIfAIsZero(t, u)

  if (_isUndefined(resultA)) return

  bar(resultA, u)
}

function doSomethingWithoutIsUndefined() {
  const t = 0
  const u = 2

  const resultA = sumButUndefinedIfAIsZero(t, u)

  if (typeof resultA === 'undefined') return

  bar(resultA, u)
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source