相关文章推荐
纯真的沙滩裤  ·  TypeScript ...·  4 天前    · 
刚毅的草稿纸  ·  TS 里将Uint8Array ...·  5 天前    · 
文质彬彬的凉面  ·  TS 下uint8array 转 ...·  5 天前    · 
宽容的铁链  ·  SQL ...·  1 年前    · 
玩篮球的手电筒  ·  你了解VB.NET ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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: 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 there Marek Urbanowicz Feb 18, 2019 at 7:33 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. LPains Feb 18, 2019 at 19:53 @НикитаЛебедев 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? Yom T. Feb 19, 2019 at 1:22

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);
                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>'
– Никита Лебедев
                Feb 18, 2019 at 8:09

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;

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

Like - http.post.toPromise()

  • You can use generic type to avoid this.

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

  • 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.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •