void
void is the type of a function whose return value the caller is not allowed to use. It is not the same as undefined, and it is not the same as never.
Detailed Explanation
In C-family languages, void means "this function returns nothing." TypeScript borrows the word and then does something slightly different with it, which is why the type surprises people.
A function typed : void may still return undefined. It may also return some other value — the checker will not complain inside the function if the return is implicit. What void actually enforces is on the caller: the returned value is not meant to be read. Assigning the result to a variable and using it is the error.
That caller-side rule is why void is the right type for callbacks you intend to ignore. array.forEach is typed (callbackfn: (value: T) => void). You can pass a function that returns a number, because a returning function is a safe stand-in for a function whose result will be thrown away. The reverse is not safe: a function typed to return number cannot accept a void function, because the caller of that function expected a number.
This is the opposite of how most other return types work, and it is deliberate. If callback parameters were checked the usual way, you could not write ids.forEach(console.log) or ids.map(parseInt)-style callbacks without wrapping them.
Contrast the three "nothing" types:
| Type | Meaning | Can the function complete? | Can the caller use the result? |
|---|---|---|---|
void | ignore the return | yes | no |
undefined | the value undefined is returned | yes | yes, it is undefined |
never | the function does not return | no | unreachable |
Under strictNullChecks, undefined is not assignable to every type, so a function that returns undefined on purpose should say so when callers need to distinguish "returned nothing" from "returned a value." void is the weaker claim: the author does not want you to look.
void as a value also exists as an operator: void expression evaluates the expression and produces undefined. You will see void fetch(...) in fire-and-forget calls, which is a way to mark a floating promise as intentionally unused. That is a runtime operator, not the type.
The cost of void is the confusion with undefined, and the fact that a : void implementation can accidentally return a value that callers are then forbidden from using. If the return is part of the contract, write the real type. If the return is leftover and should be ignored, void is the annotation.
Code Examples
1. Callers cannot use a void result, even if the body returns something.
// 1. Callers cannot use a void result, even if the body returns something.
function logId(id: string): void {
console.log(id);
return;
}
const result = logId("u_1");
// result.toString();
// Error: Property 'toString' does not exist on type 'void'.2. Why forEach accepts a callback that returns a value.
// 2. Why forEach accepts a callback that returns a value.
const ids = ["a", "b", "c"];
function print(id: string): number {
console.log(id);
return id.length;
}
ids.forEach(print); // fine — forEach is (id: string) => void, and a number is discarded
type Producer = () => number;
const producer: Producer = logId;
// Error: Type '() => void' is not assignable to type '() => number'.3. void vs undefined vs never.
// 3. void vs undefined vs never.
function implicitVoid(): void {
// no return; callers must ignore the result
}
function returnsUndefined(): undefined {
return undefined;
}
function boom(): never {
throw new Error("stop");
}
const ignored: void = implicitVoid();
const missing: undefined = returnsUndefined();
// const x: undefined = implicitVoid();
// Error under some configs: Type 'void' is not assignable to type 'undefined'.Common Gotchas
`Promise<void>` is not a forgotten return type
it means the async function resolves with no useful value. Forgetting to `return` a value from an `async` function produces `Promise<void>` by inference, which then fails to assign to `Promise<User>`.
Do not annotate a value-producing function as `void` to "silence" a return
callers lose the type, and the implementation can change what it returns without anyone noticing.
`void` in a type position is not the `void` operator
`void 0` is a runtime expression for `undefined`. `: void` is a type. They share a keyword and almost nothing else.
Returning a value from a `void` function is allowed in some positions
the checker is stricter about explicit `return expr` in a `: void` function than about passing a returning callback where `void` is expected. Do not treat those as the same rule.