TypeScript Reference ยท Primitive and Special Types
The void type in TypeScript
Learn the TypeScript void type: what it means as a return type, how it differs from undefined, and why callbacks typed void may still return values.
void is the return type of a function that does not hand back a useful value. A function such as function log(msg: string): void may finish without a return statement, or with a bare return;. Callers are told not to rely on any result.
void looks like undefined, but it is not the same. undefined is a real type with exactly one value. void says "ignore whatever comes back". That difference is why a callback typed as returning void can still be given a function that returns something.
function greet(name: string): void { console.log(`Hello, ${name}`); }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
function name(params): void { /* ... */ } type Handler = (event: Event) => void; let v: void = undefined;
| Part | Description |
|---|---|
void |
A return type meaning "no meaningful result". |
| Values | The only value you can assign to a variable of type void is undefined (and null when strictNullChecks is off). |
Edge cases:
- A function body declared
: voidcannotreturna value; the compiler reports that the type is not assignable tovoid. - A function type
() => voidaccepts functions with any return type. This is called substitutability, and it is what makesarr.forEach(x => set.add(x))compile even thoughaddreturns the set. - A function with no return annotation and no
returnvalue has the inferred return typevoid.
Usage notes
- Use
voidfor return types only. Variables, parameters and properties of typevoidare almost always a mistake. For a value that may be missing, useundefined. voidversusundefined. A function annotated(): undefinedhistorically had toreturn undefinedexplicitly (newer versions relax this). A function typed(): voidmay simply end.- Do not test the result. The compiler stops you from using a
voidvalue in a condition such asif (log()), because the result is meaningless. - Callbacks. When you write your own API that takes a callback whose result you ignore, type it
=> void. Callers can then passlist.pushstyle functions without wrapping them. If you do need the result, say so with a real return type. Promise<void>. Anasyncfunction that returns nothing has the typePromise<void>. SeePromise<T>.- Contrast with
never.nevermeans the function does not return at all (it throws or loops forever).voidmeans it returns, but with nothing useful. - Not the JavaScript operator. The
voidoperator in JavaScript (void 0) is a separate feature that evaluates an expression and yieldsundefined.
Related
undefined,never,unknown,Promise<T>- Lessons: Function types, Primitive types, any, unknown, never