TypeScript infers void instead of never on function statement

Viewed 51

TypeScript infers () => never for foo if it's the result of a function expression which doesn't return:

const foo = function () {
  throw new Error();
}

But void if foo is a function statement:

function foo() {
  throw new Error();
}

I don't get it. Would be great to have an explanation.

1 Answers

Find the explanation in the documentation.

"Never is the return type for a function expression or an arrow function expression that always throws an exception or one that never returns"

The const foo will never be assigned if the assignation throws a error.

Related