Specifying return type 'never' on function expression doesn't work, but it works on function declaration

43 Views Asked by At
function foo(): never {
  throw new Error();
}

const bar = (): never => {
  throw new Error();
}

const qux = function():never {
    throw new Error();
}

function mainFoo(a: number | undefined): number {
  if (a !== undefined) {
    return a;
  }
  foo();
}

function mainBar(a: number | undefined): number {
  if (a !== undefined) {
    return a;
  }
  bar();
}

function mainQux(a: number | undefined): number {
  if (a !== undefined) {
    return a;
  }
  qux();
}

(playground)

In the code above, mainBar and mainQux are exhibiting errors as if the return type of bar() and qux() were 'undefined'.

What's inferred differently?

enter image description here

0

There are 0 best solutions below