CodeWithMihir

TypeScript Tutorial

TypeScript void Type Explained with Examples

Learn the TypeScript void type for functions that do not return useful values, including logging, callbacks, and event handlers.

Welcome back! I am Mihir, and in this lesson we will learn the void type in TypeScript.

void is mostly used as a function return type. It means the function does not return a useful value.


What is void?

Use void when a function performs an action but does not return data.

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

This function prints a message, but it does not return anything useful.


void Function Examples

Logging:

function logError(error: string): void {
  console.error(error);
}

Updating state:

function markComplete(): void {
  console.log("Task completed");
}

Sending a notification:

function notifyUser(message: string): void {
  console.log(`Notification: ${message}`);
}

All of these functions do something, but they do not return useful values.


Returning a Value from void

This is invalid:

function getName(): void {
  return "Mihir";
}

The function says it returns void, but it returns a string.

Correct:

function getName(): string {
  return "Mihir";
}

Use void only when callers should not expect a returned value.


void with Function Types

You can use void in function type expressions.

type Logger = (message: string) => void;

const log: Logger = (message) => {
  console.log(message);
};

This says Logger is a function that accepts a string and does not return useful data.


void with Callbacks

Callbacks often return void.

function runTask(callback: () => void): void {
  callback();
}

runTask(() => {
  console.log("Task finished");
});

The callback is called for its side effect.


void with Event Handlers

Event handlers commonly return void.

function handleClick(): void {
  console.log("Button clicked");
}

In UI code, handlers usually update state, call APIs, or trigger behavior instead of returning data.


void vs undefined

undefined is a value.

let value: undefined = undefined;

void is usually used to describe a function return.

function log(): void {
  console.log("Hello");
}

For beginners, the simple rule is:

  • use void for functions that return nothing useful
  • use undefined when a variable or property can be missing

Type Inference with void

TypeScript can infer void when a function does not return.

function printCourse(title: string) {
  console.log(title);
}

TypeScript infers the return type as void.

Writing it explicitly is helpful when you want the function contract to be clear:

function printCourse(title: string): void {
  console.log(title);
}

Common Mistake: Using void for Variables

You almost never need this:

let value: void;

In regular code, use void for function returns, not normal variables.


Quick Reference Summary

ConceptExample
Void functionfunction log(): void
Callback returning voidcallback: () => void
Function type(message: string) => void
Event handlerfunction handleClick(): void
Inferred voidfunction with no return
Different from undefinedundefined is a value

Practice

Create a function that prints a course summary:

function printCourseSummary(title: string, lessons: number): void {
  console.log(`${title} has ${lessons} lessons.`);
}

printCourseSummary("TypeScript Basics", 12);

Try returning a string from this function and see what TypeScript says.


What You've Learned

You now understand:

  • What void means
  • Why void is mostly used for functions
  • How void functions differ from functions that return data
  • How to use void in callbacks
  • How event handlers commonly use void
  • The difference between void and undefined

What's Next?

In the next lesson, we will learn Arrays and Tuples in TypeScript.

We will type lists, readonly arrays, mixed arrays, and fixed-position values.


Need Help?

  • Have questions, confusion, or want to know more? Contact me

void keeps function contracts honest: this function does work, but it does not hand back data.