-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type checking not behaving as expected for Promise<T> signatures #10524
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
Comments
@rbuckton can you take a look |
The issue boils down to how TypeScript handles overloads and generic type erasure. Then we check For () => Promise<string> For (onfulfilled?: (value: boolean) => any, onrejected?: (reason: any) => any) => PromiseLike<any> When we check assignability of these signatures, they are compatible as in TypeScript you can always assign something with fewer arguments to something that expects to call it with more arguments. We will have to discuss in the team if there is a viable solution to this issue. |
Type checking is passed by the following code. /**
* Creates a new Promise with the same internal state of this Promise.
* @returns A Promise.
*/
then(): Promise<T>; It seems a not expected behavior. |
That nullary |
The problem with the The fix in #10448 uses the following overload instead: then(
onfulfilled?: ((value: T) => T | PromiseLike<T>) | undefined | null,
onrejected?: ((reason: any) => T | PromiseLike<T>) | undefined | null): Promise<T>; (yes, the Although verbose, this overload can be used as if it was |
I agree, I hope you will provide an intuitive way. |
@falsandtru The declaration above allows you to consume a promise using |
Now TypeScript is unable to detect type mismatches of Promise. declare var fooPromiseLike: PromiseLike<string>;
declare var fooPromise: Promise<string>;
declare var bar: (f: PromiseLike<boolean>) => void;
bar(fooPromiseLike); // error
bar(fooPromise); // no error, but wrong The previous case can throw errors correctly if bar(fooPromise); // error It seems there is a bug in overload processing. So I want you to fix the type checking by fixing that bug while keeping current intuitive definitions if it is a bug. |
TypeScript Version: 2.0.0@beta && 2.1.0-dev.20160824
Code
Expected behavior:
Should give an error when passed an incorrect signature, works as expected in 1.8.10. Unless I am missing something?
I couldn't find any open issues related to this and it seems to still be broken in the nightly build
The text was updated successfully, but these errors were encountered: