The return type of an async function must be the global Promise<T> type
Asked Answered
O

4

11

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

Outpoint answered 18/2, 2019 at 7:29 Comment(6)
I suppose success is not boolean type. Try to put double negation !! in the return before res... and let me know if the error is still thereAustralasia
@MU, I tried this and explicit cast tooYiyid
@MU, I think that tslint does not work correctly, but I don't know how to fix itYiyid
Did you try removing the : Promise<boolean> at all? It will implicitly type your method. It might also help you see what it is actually returning.Imagination
@НикитаЛебедев Please make sure this returned type result.data.success is indeed of boolean type and not enquoted boolean which makes it a string, like "true" instead of true. Can you please check in the Network tab of the browser console, or put a breakpoint on the method?Cns
For me it worked after removing async and also await from the function and changed the return type to false | Promise<boolean>. The function continued to work as before.Kila
C
6

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);
}
Cns answered 18/2, 2019 at 7:58 Comment(2)
Property 'resolve' does not exist on type '<T>(resolver: (resolve: (val?: T | PromiseLike<T> | undefined) => void, reject: (reason?: any) => void, notify: (progress: any) => void) => void) => Promise<T>'Yiyid
Can you please update your question with this attempted code?Cns
I
0

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;
}
Imagination answered 18/2, 2019 at 20:3 Comment(0)
A
0

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

Like - http.post.toPromise()

Alexaalexander answered 22/7, 2021 at 13:29 Comment(0)
H
0

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.

Hardening answered 24/4, 2022 at 20:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.