Never and void
Concept and syntax
Use void for a function whose result is not useful to callers. A normal void function can finish. never describes a value that cannot occur; a function declared to return never must not reach a normal return, for example because it always throws. Do not call the throwing example just to demonstrate its type. Inferred or contextual void rules for callbacks have additional nuances covered later.
Working example
Compile standalone examples in a fresh directory with tsc --strict --target ES2022 --module commonjs --noEmitOnError main.ts, then node main.js.
function report(): void { console.log("saved"); }function fail(): never { throw new Error("invalid"); }report();Expected output (one line per log):
savedCommon mistake
Intentionally invalid; check this snippet separately. Do not run it.
Expected compiler diagnostic: TS2322. Returning 1 contradicts never. If normal completion is intended, use a compatible return type.
Practice and recap
Write a void function that prints done. Then write a never function that throws and inspect it without invoking it.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Never and void — knowledge check
Check the concept, apply it to a new choice, and interpret a compiler diagnostic.
Which function can complete normally without a useful result?
Which return type describes an always-throwing function?
Why does returning 1 violate this declaration?
function fail(): never { return 1; }