Did you forget to include ‘void’ in your type argument to ‘Promise’ in Typescript?

Did you forget to include 'void' in your type argument to 'Promise' in Typescript

We have error “Did you forget to include ‘void’ in your type argument to ‘Promise'” in Typescript? To overcome this issue, we can use generics to type the value of Promise to void. Let’s find out what causes this error. Get started now for more detailed information.

Why does the error occur?

You received the error message “Did you forget to include ‘void’ in your type argument to ‘Promise'” in TypeScript because you did not pass any arguments to the Promise constructor when calling it.

Below is an example of a case when this error occurs:

function demoGetPromise() {
   return new Promise((resolve, reject) => {
    resolve();
  });
}

Error:

Expected 1 arguments, but got 0. Did you forget to include 'void' in your type argument to 'Promise'?

Did you forget to include ‘void’ in your type argument to ‘Promise’ in Typescript – How to fix it?

To get around this problem, make sure that when using the Promise constructor, you either pass it an argument or modify its parameter type. The reason you have to do is that the Promise constructor defaults to take one argument.

Change the type of the parameter

In this approach, we solve the error by passing void type to generic of Promise constructor.

Take a look at this specific example:

function demoGetPromise(){
   return new Promise<void>((resolve, reject) => {
    resolve();
  });
}

In Java, the void type is used to indicate that the function does not return any data type.

Likewise, the type void in TypeScript is used to return the type of a function where the function does not return any values.

Pass in arguments

If you don’t want to use the type void, the best way to resolve this error is to pass the Promise constructor an argument.

function demoGetPromise() {
   return new Promise((resolve, reject) => {
    const message = 'Welcome to LearnShareIT community'
    resolve(message);
  });
}

In the above example, we create a string variable message and then pass it to the resolve() function. 

If you want to resolve the value of a specific type, pass the corresponding type to the generic. For example, Promise resolved with type ‘number’:

function demoGetPromise() {
   return new Promise<number>((resolve, reject) => {
    resolve(2022);
  });
}

Moreover, use an union type in case you want Promise to get resolved with multiple types. 

function demoGetPromise() {
   return new Promise<number | string>((resolve, reject) => {
    resolve(2022);
    resolve('Welcome to LearnShareIT community')
  });
}

Summary

To sum up, the solution to avoid the error message “Did you forget to include ‘void’ in your type argument to ‘Promise’” in Typescript is to use the generic to type the Promise’s value. Try applying this method to your project and see the results. We hope the above information is helpful to you. Thank you for being so interested.

Maybe you are interested:

Leave a Reply

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