'The return type of an async function must be the global Promise<T> type

enter image description here

Why does TSlint still says "The return type of an async function or method must be the global Promise type"?

I can't understand what's wrong.

UPDATED: enter image description here



Solution 1:[1]

Try returning a Promise-wrapped value corresponding to the expected generic type of the Promise, something like so:

@Action
public async register(registerInfo: Account): Promise<boolean> {
  const res = await http.post('users/', registerInfo);

  return new Promise<boolean>((resolve, reject) => {
    resolve(res.data.success);

    // Or reject() if something wrong happened
  });

  // Or simply return a resolved Promise
  return Promise.resolve(res.data.success);
}

Actually, you should also be able to then() the result:

@Action
public async register(registerInfo: Account): Promise<boolean> {
  return await http
            .post('users/', registerInfo)
            .then(res => res.data.success);
}

Solution 2:[2]

I think the problem is that you are trying to return the result of await instead of capturing the result and processing it:

@Action
public async register(registerInfo: Account): Promise<boolean> {
  const result = await http.post('users/', registerInfo);

  return result.data.success;
}

Solution 3:[3]

The method http.post returns Observable type, you can convert it to Promise using toPromise() method.

Like - http.post.toPromise()

Solution 4:[4]

there can be 3 ways.

  1. You can use generic type to avoid this.

  2. you can check the success key before returning whether its boolean or not.

  3. As you know the result from api response will be containing result.data.success as true/false boolean value but the typescript doesn't know that(thats why there is error while showing in typescript code) so you need to explicitly define the interface maybe for your api response, there you can make the success property as boolean.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 LPains
Solution 3 Adir D
Solution 4 Himanshu Uniyal