Typescript: Function that always throws is not inferred as having `never` return type

Created on 19 Jun 2017  路  5Comments  路  Source: microsoft/TypeScript

TypeScript Version: 2.5.0-dev.20170618

Code

function fail1() {
  throw new Error()
}

const fail2 = function () {
  throw new Error()
}

Expected behavior:
Inferred return type of both functions is never

Actual behavior:
Only the return type of the second function is never. The return type of fail1 is inferred as void.

By Design

Most helpful comment

Perhaps only class methods should get void inference :rose:

All 5 comments

This is by design. We had it the way you suggest originally but I was too much of a breaking change. See #8767 for more details.

@ahejlsberg Could we have an off-by-default compiler flag that unifies this behavior?

Perhaps only class methods should get void inference :rose:

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

I really like @basarat's suggestion.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zhuravlikjb picture zhuravlikjb  路  3Comments

MartynasZilinskas picture MartynasZilinskas  路  3Comments

weswigham picture weswigham  路  3Comments

siddjain picture siddjain  路  3Comments

DanielRosenwasser picture DanielRosenwasser  路  3Comments