0Pricing
TypeScript Academy · Lesson

The void Type in Functions

How void differs from undefined in function return positions.

The void Type in Functions is a free TypeScript Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The void Return Type

void describes a function that returns no useful value. The function still completes; it just doesn't produce something you should use. It's the type of functions called for their side effects.

function log(message: string): void {
  console.log(message);
}
log('Hello void');

Inferred void

If a function has no return statement (or only bare return;), TypeScript infers void automatically. You rarely need to write it explicitly, though it documents intent.

function greet(name: string) {
  console.log('Hi ' + name);
  // no return -> inferred void
}
greet('Ada');

void vs undefined

They're related but not the same. A void function may or may not actually return undefined, and you're not supposed to rely on its return value. A function typed to return undefined must explicitly return it.

function a(): void { /* returns nothing useful */ }
function b(): undefined { return undefined; }
console.log(a(), b());

You Shouldn't Use a void Result

The runtime value of a void call is undefined, but the type system discourages using it. Treat void as 'ignore this return'.

function save(): void {
  console.log('saved');
}
const result = save(); // result has type void
console.log(result); // undefined at runtime

void in Callback Types

A subtle and powerful rule: a callback typed to return void may be satisfied by a function that returns any value. The extra return value is simply ignored.

type Handler = () => void;

const h: Handler = () => 42; // returns number, still fine
h();
console.log('Returned value is ignored by callers');

Why This Rule Exists

This flexibility lets you pass functions like arr.push (which returns a number) as void callbacks. The caller promised not to use the return value, so any return is harmless.

const items: number[] = [];
const nums = [1, 2, 3];
nums.forEach((n) => items.push(n)); // push returns number, ok
console.log(items);

Contextual void in Array Methods

forEach expects a callback returning void. That's why you can write a one-line arrow that incidentally returns a value without an error — the contextual void type absorbs it.

const result: string[] = [];
['a', 'b'].forEach((x) => result.push(x.toUpperCase()));
console.log(result);

A Common Mix-Up

The void callback rule applies to the callback type, not to functions whose own annotated return is void. A function literally declared (): void still cannot have a return value; with a non-undefined value.

function f(): void {
  return; // ok
  // return 5; // Error: 5 not assignable to void
}
f();
console.log('declared void cannot return a value');

void Parameters Are Unusual

Although rare, void can appear as a parameter type in advanced generics. In everyday code you'll almost always see it as a return type, signaling 'no meaningful result'.

type Listener = (event: string) => void;

const onClick: Listener = (e) => console.log('Event:', e);
onClick('click');

void With Promises

Async functions often return Promise<void> when they perform work but resolve with no value. The pattern mirrors synchronous void for asynchronous code.

async function sync(): Promise<void> {
  console.log('syncing...');
  // resolves with no value
}
sync().then(() => console.log('done'));

Choosing void Deliberately

Annotate void when you want to communicate that a function exists for its side effects and callers must not depend on a return value. It clarifies intent and lets the flexible callback rule work for you.

function notify(msg: string): void {
  console.log('NOTIFY:', msg);
}
const handlers: Array<(m: string) => void> = [notify];
handlers.forEach((fn) => fn('ping'));

Quick Check

Test your understanding of the void type.

Recap: void

You learned that void:

  • Marks functions that return no useful value.
  • Differs from undefined: void results shouldn't be used.
  • As a callback type, accepts functions that return any value (ignored by callers).
  • Enables clean use of methods like push inside forEach.

Next, we put it all together: safely handling unknown.

const tasks: Array<() => void> = [
  () => console.log('task 1'),
  () => console.log('task 2')
];
tasks.forEach((t) => t());

Frequently asked questions

Is the “The void Type in Functions” lesson free?

Yes — the full text of “The void Type in Functions” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “The void Type in Functions”?

How void differs from undefined in function return positions. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “The void Type in Functions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Understanding unknown vs any
  2. The never Type and Impossible States
  3. The void Type in Functions
  4. Type-Safe Handling of unknown
← Back to TypeScript Academy