How to typehint promises in typescript
See original GitHub issueI 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:
- Created 7 years ago
- Reactions:5
- Comments:13 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Fixed via 3704d57
but you don’t specify the exact type returned by
AxiosResponse
so it’sany
implicitly