Better Support for never-Returning Functions
As part of the work for assertion signatures, TypeScript needed to encode more about where and which functions were being called.This gave us the opportunity to expand support for another class of functions: functions that return never
.
The intent of any function that returns never
is that it never returns.It indicates that an exception was thrown, a halting error condition occurred, or that the program exited.For example, process.exit(…)
in @types/node
is specified to return never
.
In order to ensure that a function never potentially returned undefined
or effectively returned from all code paths, TypeScript needed some syntactic signal - either a return
or throw
at the end of a function.So users found themselves return
-ing their failure functions.
function dispatch(x: string | number): SomeType {
if (typeof x === "string") {
return doThingWithString(x);
}
else if (typeof x === "number") {
return doThingWithNumber(x);
}
return process.exit(1);
}
Now when these never
-returning functions are called, TypeScript recognizes that they affect the control flow graph and accounts for them.
function dispatch(x: string | number): SomeType {
if (typeof x === "string") {
return doThingWithString(x);
}
else if (typeof x === "number") {
return doThingWithNumber(x);
}
process.exit(1);
}
As with assertion functions, you can read up more at the same pull request.