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.

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:
voidmeans "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.undefinedmeans "The value is literally theundefinedprimitive." 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.
TYPESCRIPTinterface 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
| Feature | void | undefined |
|---|---|---|
| Primary Use | Function return types | Optional properties, missing values |
| Is it a value? | No, it's a type signal | Yes, 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.
- Change a function that does not return a value to explicitly return
void. - Add an optional property
completedAtto yourTaskinterface. - Try to assign
nullto that property—notice how TypeScript complains ifstrictNullChecksis enabled—then assignundefinedto see it accepted.
Common Pitfalls
- Returning a value from a
voidfunction: TypeScript will allow it if you are in a non-strict environment, but it is dangerous. Always aim for strict type checking. - Confusing
voidandundefinedin callbacks: A callback typed as returningvoidcan safely return any value (the value is simply ignored). A callback typed as returningundefinedmust explicitly returnundefined. - Overusing
undefined: Use?for optional properties rather than unioning withundefinedmanually, 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.
Work with me

Next.js Full-Stack Web App Development
A fast, SEO-ready full-stack web app built with Next.js 16 — from idea to deployed product, by an engineer who ships to production.

React & Next.js Dashboard / Admin UI Development
A clean, data-rich dashboard UI in React or Next.js — charts, tables, and real-time data that your users will actually enjoy using.
