Void data type in TypeScript

The void data type in Typescript represents the return value of functions which don’t return a value or return statement does not return anything.

Void Data Type

The following example someFunc1 does not have 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 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 to false is not a recommended practise.

Void as function parameter

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

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

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 value undefined when the function does not return any value.

Why use void

When you that the function does not return any value, then it is better to annotate it with void. This will prevent you from accidently 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 makes 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 useful for advanced callback patterns

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

Now let us create three function of type voidFunc. f1 which returns 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 function to return void, but instead ignores them. But it still infers the return type of these functions as void.

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

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

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

You can also use unknown type.

void is pretty useful in callback functions where callback function might return some value.

References

  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.

Scroll to Top