Back to Blog
Lesson 11 of the TypeScript: Typing JavaScript with Confidence course
TypeScriptJuly 29, 20264 min read

Understanding Void and Undefined in TypeScript

Stop confusing void and undefined. Learn how to use them correctly in TypeScript to ensure type safety and avoid common runtime bugs in your applications.

TypeScriptType SafetyVoidUndefinedWeb Development
A moody view of a dimly lit urban architectural corridor, creating a dramatic and mysterious atmosphere.

Previously in this course, we covered Defining Function Return Types in TypeScript: A Practical Guide. While that lesson taught us how to tell TypeScript what a function should return, it left a common point of confusion: when should we use void, and when should we use undefined?

In JavaScript, these two concepts often blur together. In TypeScript, they represent two fundamentally different intentions. Using them correctly is a core skill for writing production-grade code that doesn't crash at runtime.

The First Principles of Void vs. Undefined

At a high level, the difference boils down to intent:

  • void means "I don't care about the return value." It is a signal to the type checker that a function performs a side effect (like logging to the console or updating a DOM element) and returns nothing of interest.
  • undefined means "The value is literally the undefined primitive." It is a real value that exists in JavaScript, often representing an optional field or a missing variable.

If you declare a function return as void, TypeScript will prevent you from accidentally using the result of that function. If you declare it as undefined, you are explicitly promising that the function will return an actual undefined value.

Using Void for Return Types

Use void when you are performing actions that do not produce output for the caller. In our task API client, this is common for logger functions or side-effect-heavy utility functions.

TYPESCRIPT
// Correct: This function performs an action but doesn't return data
function logTaskStatus(taskId: string): void {
  console.log(CE9178">`Processing task: ${taskId}`);
  // No return statement needed
}

If you try to return a value from this function, TypeScript will throw an error, preventing you from accidentally leaking data where it isn't expected.

Using Undefined for Optional Properties

While void is for function signatures, undefined is for data structures. When defining our Task interface, we often encounter fields that might not exist yet, such as an assignedTo field.

TYPESCRIPT
interface Task {
  id: string;
  title: string;
  // This property might not exist
  assignedTo?: string | undefined; 
}

Note that in modern TypeScript, the ? modifier (the optional property shorthand we explored in Handling Optional Properties in TypeScript: A Practical Guide) already includes undefined in the type. Explicitly adding | undefined is sometimes helpful for clarity, but the behavior remains the same: the value can be missing or explicitly set to undefined.

Comparison Table

Featurevoidundefined
Primary UseFunction return typesOptional properties, missing values
Is it a value?No, it's a type signalYes, it is a primitive value
Logic"I won't return anything""I might return nothing/missing"

Hands-on Exercise

Open your task API client project. Locate your Task interface and your utility functions.

  1. Change a function that does not return a value to explicitly return void.
  2. Add an optional property completedAt to your Task interface.
  3. Try to assign null to that property—notice how TypeScript complains if strictNullChecks is enabled—then assign undefined to see it accepted.

Common Pitfalls

  1. Returning a value from a void function: TypeScript will allow it if you are in a non-strict environment, but it is dangerous. Always aim for strict type checking.
  2. Confusing void and undefined in callbacks: A callback typed as returning void can safely return any value (the value is simply ignored). A callback typed as returning undefined must explicitly return undefined.
  3. Overusing undefined: Use ? for optional properties rather than unioning with undefined manually, as it makes your type definitions cleaner and more idiomatic.

FAQ

Can I set a variable to void? No. void is not a value; you cannot assign it to a variable. You can only use it to describe the signature of a function.

Does void mean the function returns null? No. null is a value, and void is a type signal that no useful value is returned. They are not interchangeable.

Why does my void function still return undefined at runtime? In JavaScript, all functions return undefined by default if no return statement is present. TypeScript's void type is a compile-time guard, not a runtime change to how JavaScript functions behave.

Recap

We have established that void is for side-effect-heavy return types, while undefined represents a missing or optional value. By keeping these distinct, you avoid the common bugs associated with "leaky" functions and unexpected undefined access. This distinction is critical for TypeScript data normalization: fixing undefined errors with mapped types as your application grows in complexity.

Up next: Asynchronous Functions and Promises — we'll see how these concepts interact with network requests in our API client.

Similar Posts