Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to typehint promises in typescript #480

Closed
macnibblet opened this issue Oct 13, 2016 · 13 comments
Closed

How to typehint promises in typescript #480

macnibblet opened this issue Oct 13, 2016 · 13 comments
Assignees

Comments

@macnibblet
Copy link

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:...'.

@croes
Copy link

croes commented Oct 25, 2016

Ran into the same error. The Promise in axios.d.ts (https://github.com/mzabriskie/axios/blob/master/axios.d.ts#L61) seems to be incompatible with the ES6 Promise defined in lib.es6.d.ts (https://github.com/Microsoft/TypeScript/blob/master/lib/lib.es6.d.ts#L1356).

@nickuraltsev nickuraltsev self-assigned this Oct 26, 2016
@pmlamotte
Copy link

pmlamotte commented Nov 2, 2016

Ran into a similar error that is breaking my vscode setup. My hacky workaround right now was to copy the axios typings locally, remove the promise definition, and reference it through includes in .tsconfig. I then had to manually typecast the return values to make it happy.

axios.post(
    ...
).then(response => response.data as User) as Promise<User>

When I modified the node_modules/axios/axios.d.ts file directly to remove the Promise definition none of that typecasting was necessary since I'm already making use of es2015 promise definitions.

I'm totally new to typescript so I'm not sure what the appropriate fix might be.

@rafaelbiten
Copy link

I'm also new to TS and I've been trying to use it on a very small/simple project.

I'm really confused on how to use TypeScript without using 'any' or getting any TS errors/warnings.

screen shot 2016-11-03 at 10 23 51 am

I tried many different things, but the image above was probably my best attempt but for some reason I still get the Type 'void' is not assinable to type 'string' error. I was wondering if this could be related to this bug.

If I use (u: string): Axios.IPromise<any>, it fixes the issue, but I'm trying to avoid using 'any'.

Tks!

@DenisCarriere
Copy link
Contributor

DenisCarriere commented Nov 3, 2016

If you use async you'll be able to return a Promise<void> from that function, I don't think this issue has anything to do with Axios.

I was able to run this function without any Typescript errors using the default axios.d.ts definition.

async authenticate(credentials: AuthenticationCredentials): Promise<void> {
  const response = await axios.post('backend://authentication')
  this.processToken(response.data.token)
}

@dpoineau dpoineau mentioned this issue Jan 5, 2017
2 tasks
@mktange
Copy link

mktange commented Jan 11, 2017

This is fixed with the following PR #588

@gluons
Copy link

gluons commented Jan 15, 2017

Same here.
Promise from then is not compatible with native Promise.

error TS90010: Type 'Promise<void>' is not assignable to type 'Promise<void>'. Two different types with
 this name exist, but they are unrelated.
  Types of property 'then' are incompatible.

@acheshkov
Copy link

+1

@retwedt
Copy link

retwedt commented Feb 28, 2017

+1 any progress/solutions on this?

@shousper
Copy link

+1

@nickuraltsev
Copy link
Member

Fixed via 3704d57

jimthedev pushed a commit to commitizen/cz-cli that referenced this issue May 24, 2018
This Pull Request updates dependency [axios](https://github.com/axios/axios) from `v0.15.2` to `v0.18.0`



<details>
<summary>Release Notes</summary>

### [`v0.18.0`](https://github.com/axios/axios/blob/master/CHANGELOG.md#&#8203;0180-Feb-19-2018)
[Compare Source](axios/axios@v0.17.1...v0.18.0)
- Adding support for UNIX Sockets when running with Node.js ([#&#8203;1070](`axios/axios#1070))
- Fixing typings ([#&#8203;1177](`axios/axios#1177)):
    - AxiosRequestConfig.proxy: allows type false
    - AxiosProxyConfig: added auth field
- Adding function signature in AxiosInstance interface so AxiosInstance can be invoked ([#&#8203;1192](`axios/axios#1192), [#&#8203;1254](`axios/axios#1254))
- Allowing maxContentLength to pass through to redirected calls as maxBodyLength in follow-redirects config ([#&#8203;1287](`axios/axios#1287))
- Fixing configuration when using an instance - method can now be set ([#&#8203;1342](`axios/axios#1342))

---

### [`v0.17.1`](https://github.com/axios/axios/blob/master/CHANGELOG.md#&#8203;0171-Nov-11-2017)
[Compare Source](axios/axios@v0.17.0...v0.17.1)
- Fixing issue with web workers ([#&#8203;1160](`axios/axios#1160))
- Allowing overriding transport ([#&#8203;1080](`axios/axios#1080))
- Updating TypeScript typings ([#&#8203;1165](`axios/axios#1165), [#&#8203;1125](`axios/axios#1125), [#&#8203;1131](`axios/axios#1131))

---

### [`v0.17.0`](https://github.com/axios/axios/blob/master/CHANGELOG.md#&#8203;0170-Oct-21-2017)
[Compare Source](axios/axios@v0.16.2...v0.17.0)
- **BREAKING** Fixing issue with `baseURL` and interceptors ([#&#8203;950](`axios/axios#950))
- **BREAKING** Improving handing of duplicate headers ([#&#8203;874](`axios/axios#874))
- Adding support for disabling proxies ([#&#8203;691](`axios/axios#691))
- Updating TypeScript typings with generic type parameters ([#&#8203;1061](`axios/axios#1061))

---

### [`v0.16.2`](https://github.com/axios/axios/blob/master/CHANGELOG.md#&#8203;0162-Jun-3-2017)
[Compare Source](axios/axios@v0.16.1...v0.16.2)
- Fixing issue with including `buffer` in bundle ([#&#8203;887](`axios/axios#887))
- Including underlying request in errors ([#&#8203;830](`axios/axios#830))
- Convert `method` to lowercase ([#&#8203;930](`axios/axios#930))

---

### [`v0.16.1`](https://github.com/axios/axios/blob/master/CHANGELOG.md#&#8203;0161-Apr-8-2017)
[Compare Source](axios/axios@v0.16.0...v0.16.1)
- Improving HTTP adapter to return last request in case of redirects ([#&#8203;828](`axios/axios#828))
- Updating `follow-redirects` dependency ([#&#8203;829](`axios/axios#829))
- Adding support for passing `Buffer` in node ([#&#8203;773](`axios/axios#773))

---

### [`v0.16.0`](https://github.com/axios/axios/blob/master/CHANGELOG.md#&#8203;0160-Mar-31-2017)
[Compare Source](axios/axios@v0.15.3...v0.16.0)
- **BREAKING** Removing `Promise` from axios typings in favor of built-in type declarations ([#&#8203;480](`axios/axios#480))
- Adding `options` shortcut method ([#&#8203;461](`axios/axios#461))
- Fixing issue with using `responseType: 'json'` in browsers incompatible with XHR Level 2 ([#&#8203;654](`axios/axios#654))
- Improving React Native detection ([#&#8203;731](`axios/axios#731))
- Fixing `combineURLs` to support empty `relativeURL` ([#&#8203;581](`axios/axios#581))
- Removing `PROTECTION_PREFIX` support ([#&#8203;561](`axios/axios#561))

---

### [`v0.15.3`](https://github.com/axios/axios/blob/master/CHANGELOG.md#&#8203;0153-Nov-27-2016)
[Compare Source](axios/axios@v0.15.2...v0.15.3)
- Fixing issue with custom instances and global defaults ([#&#8203;443](`axios/axios#443))
- Renaming `axios.d.ts` to `index.d.ts` ([#&#8203;519](`axios/axios#519))
- Adding `get`, `head`, and `delete` to `defaults.headers` ([#&#8203;509](`axios/axios#509))
- Fixing issue with `btoa` and IE ([#&#8203;507](`axios/axios#507))
- Adding support for proxy authentication ([#&#8203;483](`axios/axios#483))
- Improving HTTP adapter to use `http` protocol by default ([#&#8203;493](`axios/axios#493))
- Fixing proxy issues ([#&#8203;491](`axios/axios#491))

---

</details>




---

This PR has been generated by [Renovate Bot](https://renovatebot.com).
@leemove
Copy link

leemove commented Sep 4, 2018

It does't work!

@luciomartinez
Copy link

This worked for me:

import axios, {AxiosPromise} from 'axios';

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

@vileen
Copy link

vileen commented 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

@axios axios locked and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests