Void data type in TypeScript

The void data type in Typescript represents the return value of functions that don’t return a value or return statements that do not return anything.

Void Data Type

The following example someFunc1 does not have a return statement. someFunc2 does have a return statement but does not return anything. In such cases, the Typescript infers the type as void.

You can also annotate the return type explicitly to void.

Assigning to void

Nothing is assignable to void except voidundefined & null. You can assign null to void only if strictNullChecks is set to false in tsconfig.json

You cannot assign any other value to it. It will throw a Type is not assignable to type ‘void’ error.

But you cannot assign void to undefined

Functions in JavaScript return undefined if they do not return any values.

To assign null first make the strictNullChecks false in tsconfig.ts. Remember that making it false is not a recommended practice.

Void as a function parameter

You can use void as the function parameter. The only valid values that you can pass are void, undefined & null (strictNullChecks is set to false in tsconfig.json)

The underlying JavaScript neither has a data type void nor value.

The void data type can hold only one value i.e. undefined (or null if the strictNullChecks compiler option is off). That is because JavaScript returns a value undefined when the function does not return any value.

Why use void

When you know that the function does not return any value, then it is better to annotate it with the void. This will prevent you from accidentally returning a value.

It also prevents you from storing and doing something with the return value.

Similarities with Undefined

But you can also use undefined instead of void with the same effect.

void vs undefined

The above examples make it appear that void is almost similar to undefined. But they have one difference.

If a function type specifies return type void when implemented, can return any other value, but it will be ignored. The behavior is helpful for advanced callback patterns

For Example, let us create a function type voidFunc, whose return type is void.

Now let us create three functions of type voidFunc. f1 which returns a number, f2 which returns a boolean and f3 which returns a string. Although the voidFunc returns void, the Typescript compiler does not enforce f1, f3 & f3 functions to return void, but ignores them. But it still infers the return type of these functions as void.

i.e. although v1 has value 10 the compiler treats it as of type void and hence does not allow us to use it for any operations.

Change the type to undefined and the compiler complaints. Since the voidFunc returns undefined, Typescript compiler also expects a function f1 also, return undefined.

The code above also works with the any, but it defeats the purpose using TypeScript.

You can also use an unknown type.

void is pretty useful in callback functions, which might return some value.


  1. Avoid the Void
  2. Basic Types Void
  3. Why is undefined assignable to void?
  4. Typescript Substitutability

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top