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

In 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 for voidundefined, null, any & never. 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.

Vs Any

You can also assign variable of any type to a void variable.

A variable of any type can be assigned to anything. This is because the compiler switches type checking when any is involved.

Vs never

The never type looks very similar to void.

We use void when the function does return but does not return a value. The typescript infers the return value as void. Use never return type when the function does not return at all. 

You can refer to the article void Vs never.

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 returns no 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, it 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,f2 & f3). f1 returns a number, f2 returns a boolean, and f3 returns a string. Although the voidFunc returns void, the Typescript compiler does not enforce f1, f2 & 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 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, the Typescript compiler also expects a function f1 to 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.

References

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

2 thoughts on “Void data type in TypeScript”

  1. “You cannot assign any other value to it.”
    – Actually we do can assign “any” type to void. We can assign “any”, “void”, “undefined” and even “never”. Also “null” if strictNullChecks: false.

    let a:void
    a=”test” as any

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