Imagine a scenario where I am working on a Typescript project that includes the latest packages:
In this project, I define a function that returns a Promise based on Typescript's native ambient declarations:
import * as q from "q";
function doSomethingElseAsync(): Promise<number> {
return q.Promise<number>((resolve, reject) => {
setTimeout(() => resolve(1), 5000);
});
}
During compilation, an error is thrown by Typescript stating:
error TS2322: Type 'Q.Promise<number>' is not assignable to type 'Promise<number>'.
Types of property 'then' are incompatible.
Type '<U>(onFulfill?: ((value: number) => IWhenable<U>) | undefined, onReject?: ((error: any) => IWhena...' is not assignable to type '<TResult1 = number, TResult2 = never>(onfulfilled?: ((value: number) => TResult1 | PromiseLike<TR...'.
Types of parameters 'onFulfill' and 'onfulfilled' are incompatible.
Type '((value: number) => TResult1 | PromiseLike<TResult1>) | null | undefined' is not assignable to type '((value: number) => IWhenable<TResult1 | TResult2>) | undefined'.
Type 'null' is not assignable to type '((value: number) => IWhenable<TResult1 | TResult2>) | undefined'.
Initially thinking it was due to Q Promises not being compatible with Typescript's declarations, the error disappeared completely when adding the async
keyword to the function definition.
This peculiar behavior raises questions about whether this is a bug in Typescript, Q, or the Q typings, or if it is an expected but intricate feature of the compiler.