TypeScript Survival Guide

Favorite typescript videos

052 — #18 Never Type in TypeScript | TypeScript Basics & Data Types | A Complete TypeScript Course

The lecture introduces `never`: a return type meaning the function will not return anything, ever — not even `undefined`. Setting `never` on the ordinary `greetUser` function errors ("a function returning never cannot have a reachable end point") because it can fall off the end; setting it on `createError(message: string, code: number)`, which `throw`s an error object instead of returning, compile

Video: 052 · #18 Never Type in TypeScript | TypeScript Basics & Data Types | A Complete TypeScript Course · watch · ~09:25 · procademy

Source: this is a written companion to the video. Part of the Favorite TypeScript Videos curated path.

The lecture introduces never: a return type meaning the function will not return anything, ever — not even undefined. Setting never on the ordinary greetUser function errors (“a function returning never cannot have a reachable end point”) because it can fall off the end; setting it on createError(message: string, code: number), which throws an error object instead of returning, compiles fine. The observable difference: logging a void call prints undefined, while logging a never call prints nothing at all — the throw happens first. Two practical notes close it out: never must be written explicitly (an unannotated throwing function is inferred void), and infinite-loop functions are the second canonical never case.

never means no reachable end point

Setting greetUser’s return type to never errors — hovering reads “a function returning never cannot have a reachable end point.”

function greetUser(name: string): void {
	console.log("Hello " + name);
}
greetUser("John"); // Hello John

greetUser returns nothing but can reach its end, so its honest type is void. never promises stricter: the end is never reached.

The throwing function — createError

function createError(errorMessage: string, errorCode: number): never {
	throw { message: errorMessage, code: errorCode };
	// console.log(errorMessage); // unreachable — never runs
}

The function only throws — anything after the throw never runs because the function exits immediately. Calling createError("Internal server error", 500) compiles cleanly, logs “Hello John” from the earlier line, then surfaces the thrown { message, code } object as an uncaught error (thrown, not returned).

Observable difference — void logs undefined, never logs nothing

console.log(greetUser("John")); // Hello John, then undefined
console.log(createError("Page not found", 404)); // throws; logs nothing

Logging a void call prints undefined; logging a never call prints nothing, because the throw happens first. The recap: void at least returns undefined, while never never returns at all. The demo even needs the earlier throwing calls commented out first, since a throw stops everything after it

never must be explicit; infinite loops qualify too

Without an explicit annotation, a throwing function’s return type is inferred as void — never must be written out.

function infinite(): never {
	while (true) {
		// keeps iterating infinitely — never returns
	}
}

A while (true) loop that iterates forever is the second canonical never case: the function never returns, so never is its honest return type. Closing rule: use never for the return type of a function that is never going to return.

editor showing the greetUser function and a

Transcript corrections applied above: void (not white, wide (return type)); greetUser (not great user, greed user, create user); comment out this code/call (not commend this code/call); app.ts file (not sub.ts file); while loop (not y loop); never type (not nevertype); console.log statement (not console.logstatement).

Prev: 051 — #17 Unknown Type in TypeScript | TypeScript Basics & Data Types | A Complete · Next: 053 — #19 Configuring TypeScript Compiler | Configuring TypeScript Compiler | A Complete TypeScript Course