Búsqueda de sitios web

typescript - Especificar el tipo de retorno 'never' en una expresión de función no funciona, pero funciona en la declaración de función


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();
}

(patio de recreo)

En el código anterior, mainBar y mainQux están mostrando errores como si el tipo de retorno de bar() y qux() fuera 'undefined'.

¿Qué se infiere de manera diferente?