question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to typehint promises in typescript

See original GitHub issue

I currently have the following code


  authenticate(credentials: AuthenticationCredentials): Promise<void> {
    return axios
      .post('backend://authentication', credentials)
      .then<void>(response => {
        this.processToken(response.data.token);
      });
  }

The error it produces

Type 'Promise<void>' is not assignable to type 'Promise<void>'.
  Types of property 'then' are incompatible.
    Type '{ <R1, R2>(onFulfilled: (value: void) => R1 | Promise<R1>, onRejected: (error: any) => R2 | Promi...' is not assignable to type '{ <TResult1, TResult2>(onfulfilled: (value: void) => TResult1 | PromiseLike<TResult1>, onrejected...'.
      Type 'Promise<any>' is not assignable to type 'Promise<any>'.
        Types of property 'then' are incompatible.
          Type '{ <R1, R2>(onFulfilled: (value: any) => R1 | Promise<R1>, onRejected: (error: any) => R2 | Promis...' is not assignable to type '{ <TResult1, TResult2>(onfulfilled: (value: any) => TResult1 | PromiseLike<TResult1>, onrejected:...'.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
nickuraltsevcommented, Mar 15, 2017

Fixed via 3704d57

2reactions
vileencommented, Apr 19, 2019

This worked for me:

import axios, {AxiosPromise} from 'axios';

function get(): AxiosPromise {
  return axios.get('http://my.domain/api');
}

but you don’t specify the exact type returned by AxiosResponse so it’s any implicitly

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript promise generic type - Stack Overflow
The generic type of the Promise should correspond to the non-error return-type of the function. The error is implicitly of type any and...
Read more >
Learn How does TypeScript Promise type work? - eduCBA
In TypeScript, promise type takes an inner function, which further accepts resolve and rejects as parameters. Promise accepts a callback function as parameters, ......
Read more >
Keep Your Promises in TypeScript using async/await
Promises in TypeScript. We begin by creating a simple promise like this: const one = new Promise<string>((resolve, reject) => ...
Read more >
How to type an async Function in TypeScript | bobbyhadz
To type an async function in TypeScript, set its return type to `Promise `. Functions marked `async` are guaranteed to return a `Promise`...
Read more >
Documentation - More on Functions - TypeScript
The problem is that the function promises to return the same kind of object as was passed in, not just some object matching...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found