AI Hints
Home›TypeScript Tutorial›More About Functions
TypeScript · Lesson 18 of 120

void and never

Nothing, and never.

⏱ 2 min read▶ Video at 14:28🎓 Free
Watch this lesson in the TypeScript courseWatch on YouTube ↗Full TypeScript course

Some functions return nothing useful. Their return type is void. Others never return at all, because they always throw an error. Their type is never.

void and never
From the video: void and never
  • void: nothing useful
  • never: never returns
  • Two special types

void and never

Log order returns void, it only prints. Fail returns never, it always throws. We call log order, then fail inside a try. Run it.

app.tstypescript
function logOrder(id: number): void {
  console.log("Order", id, "logged");
}

function fail(message: string): never {
  throw new Error(message);
}

logOrder(1042);
try {
  fail("Payment declined");
} catch (e) {
  console.log("Caught:", (e as Error).message);
}
Output
Order 1042 logged
Caught: Payment declined

Where you see them

Void is the most common, for event handlers and logging. Never shows up in functions that throw, and later, in exhaustive checks, where it proves every case is handled.

keyvalue
voidlogging, handlers
neveralways throws
Laterexhaustive checks
Rarein everyday code

Summary

Void means nothing useful comes back. Never means the function never returns. Next, object types.

Key takeaways
  • void
  • never
  • throw
Next up: Object Types — shapes