Sub topic 12
void and never
This chapter assumes you can annotate functions with parameter and return types, read union types, and use switch statements. You should also know that a function with no return statement hands back undefined at runtime.
void and never describe what a function gives back to its caller. void means the function finishes normally but produces no useful value. never means the function never finishes normally, because it throws an error or loops forever. The diagram below shows how to choose between them.
void for functions with no result
A void return type tells the compiler that callers should ignore the result. The function may still run code with side effects, such as logging or updating state.
function logMessage(message: string): void {
console.log(message);
}
const result = logMessage("saved"); // result has type voidA void variable can only hold undefined or null. Use void for return types, not for storing values.
Callbacks behave differently. A function type that returns void still accepts a function that returns a value. TypeScript lets you pass () => 42 where () => void is expected, and the returned number is discarded. This rule keeps APIs like Array.prototype.forEach flexible.
never for paths that cannot finish
A function whose return type is never must not reach its end. Throwing an error is the usual way to do that.
function fail(reason: string): never {
throw new Error(reason);
}
function parsePort(input: string): number {
const port = Number(input);
if (Number.isNaN(port)) {
return fail(`Invalid port: ${input}`);
}
return port;
}never is also useful for exhaustive checks. When every case of a union is handled, the leftover value has type never. If someone later adds a new case, the assignment below fails to compile.
type Status = "idle" | "loading" | "done";
function label(status: Status): string {
switch (status) {
case "idle":
return "Waiting";
case "loading":
return "Working";
case "done":
return "Finished";
default: {
const unreachable: never = status;
throw new Error(`Unhandled status: ${unreachable}`);
}
}
}Common errors
- Writing
: voidand then returning a value in a named function declaration. The compiler reports this as an error, so change the return type to match the value. - Expecting a
voidcallback to reject a value-returning function. It accepts it. - Declaring
neveron a function that can return normally. The compiler reports an error at the function's end. - Assuming
neverdisappears from a union. Members typedneverare removed from a union, which is why narrowing can leaveneverbehind when cases are missing.
> Note: Use never for the default branch of a switch when you need the compiler to flag missing cases.
Exercise
Why does the compiler accept const handlers: Array<() => void> = [() => 42];, and what would change if you annotated the callback type as () => number instead?
Show answer
A () => void type accepts any function with no parameters, because the return value of a void callback is ignored. Changing the element type to () => number would make the array hold functions that must return a number. The () => 42 callback would still compile in that case, but the array's type would then promise a return value the caller can use.