Sub topic 10
`never`: The Type With No Values
This chapter assumes you know TypeScript annotations, union types, and basic control flow. It also assumes you can run the TypeScript compiler or use an editor that reports type errors.
What `never` Means
never is the bottom type in TypeScript. It describes values that can never exist.
A variable with type never cannot receive any value:
let impossible: never;
// impossible = 1; // Error
// impossible = "text"; // Error
// impossible = false; // ErrorThe type unknown can represent any value, so it is called a top type. The type never represents no values, so it is called a bottom type.
| Type | Values it can represent | Common use |
|---|---|---|
| `unknown` | Any JavaScript value | Unchecked external data |
| `never` | No values | Impossible states and unreachable code |
never is different from void. A function returning void finishes normally but does not return a useful value. A function returning never does not finish normally.
Functions That Return `never`
A function has return type never when it cannot return to its caller. It might always throw an error or run forever.
function fail(message: string): never {
throw new Error(message);
}
function stopForever(): never {
while (true) {
// The loop never ends.
}
}The fail function throws on every call. The stopForever function contains an infinite loop. Neither function produces a return value.
This information affects control-flow analysis. After a call to a never-returning function, TypeScript knows that normal execution cannot continue.
function getPort(value: string | undefined): string {
if (value === undefined) {
return fail("PORT is required");
}
return value;
}
console.log(getPort("3000"));Inside the final return, value is known to be a string. The undefined case was handled, and the error function cannot return and leave that case unresolved.
Impossible Control-Flow Paths
TypeScript can narrow a variable until no possible type remains. At that point, the variable has type never.
For example, this function handles every member of a union:
type Result =
| { status: "ok"; value: string }
| { status: "error"; message: string };
function describe(result: Result): string {
if (result.status === "ok") {
return `Value: ${result.value}`;
}
if (result.status === "error") {
return `Problem: ${result.message}`;
}
return result;
}The final return is unreachable according to the declared Result type. In that location, result is narrowed to never. The compiler knows that no Result value can have a status other than "ok" or "error".
This is useful because it exposes incorrect assumptions. If another variant is later added to Result, the final path will no longer be impossible. A deliberate never check can then make the missing handling visible during compilation.
`never` and Assignability
Because never has no values, a value of type never can be assigned to any other type. There is no actual value that could violate the destination type.
function failNumber(): never {
throw new Error("No number exists");
}
const count: number = failNumber();
const name: string = failNumber();Both assignments compile. The function never produces a value, so neither assignment receives an invalid value.
The reverse does not work. A string, number, or unknown value might exist, so TypeScript cannot assign it to never.
Summary
neverrepresents no possible values.- A
never-returning function always throws, exits abnormally, or never ends. - TypeScript uses
neverwhen control-flow narrowing removes every possible union member. neveris useful for detecting impossible paths and later supports exhaustive union checks.neveris not the same asvoid, which describes a function that finishes without a useful result.
Exercise
What return type should fail(message: string) use if it always throws, and why?
Show answer
It should use never because the function cannot complete normally or return a value.