Skip to content

Weaken the definition of PromiseLike to allow 'T | awaited T' #37570

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1378,20 +1378,25 @@ declare type PropertyDecorator = (target: Object, propertyKey: string | symbol)
declare type MethodDecorator = <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
declare type ParameterDecorator = (target: Object, propertyKey: string | symbol, parameterIndex: number) => void;

declare type PromiseConstructorLike = new <T>(executor: (resolve: (value?: T | PromiseLike<T> | awaited T) => void, reject: (reason?: any) => void) => void) => PromiseLike<T>;
declare type PromiseConstructorLike = new <T>(executor: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void) => PromiseLike<T>;

/**
* A "Promise-like" is an object that has a callable `then` method that accepts fulfillment and rejection handlers.
* A "Promise-like" implementation may or may not be compatible with native ES2015 or Promise/A+, but provides a similar-
* enough implementation that a native of Promise/A+ -compatible promise implementation could adopt its result.
*/
interface PromiseLike<T> {
/**
* Attaches callbacks for the resolution and/or rejection of the Promise.
* @param onfulfilled The callback to execute when the Promise is resolved.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of which ever callback is executed.
*/
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: awaited T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): PromiseLike<awaited TResult1 | awaited TResult2>;
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T | awaited T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): PromiseLike<TResult1 | TResult2>;
}

/**
* Represents the completion of an asynchronous operation
* Represents the completion of an asynchronous operation.
*/
interface Promise<T> {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration1
tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(10,23): error TS1055: Type 'typeof Thenable' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
Construct signature return types 'Thenable' and 'PromiseLike<T>' are incompatible.
The types returned by 'then(...)' are incompatible between these types.
Type 'void' is not assignable to type 'PromiseLike<awaited TResult1 | awaited TResult2>'.
Type 'void' is not assignable to type 'PromiseLike<TResult1 | TResult2>'.
tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(17,16): error TS1058: The return type of an async function must either be a valid promise or must not contain a callable 'then' member.
tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration15_es5.ts(23,25): error TS1320: Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member.

Expand Down Expand Up @@ -39,7 +39,7 @@ tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration1
!!! error TS1055: Type 'typeof Thenable' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.
!!! error TS1055: Construct signature return types 'Thenable' and 'PromiseLike<T>' are incompatible.
!!! error TS1055: The types returned by 'then(...)' are incompatible between these types.
!!! error TS1055: Type 'void' is not assignable to type 'PromiseLike<awaited TResult1 | awaited TResult2>'.
!!! error TS1055: Type 'void' is not assignable to type 'PromiseLike<TResult1 | TResult2>'.
async function fn7() { return; } // valid: Promise<void>
async function fn8() { return 1; } // valid: Promise<number>
async function fn9() { return null; } // valid: Promise<any>
Expand Down
20 changes: 2 additions & 18 deletions tests/baselines/reference/awaitedVariance.errors.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
tests/cases/conformance/types/awaited/awaitedVariance.ts(74,1): error TS2322: Type 'C<Promise<number>>' is not assignable to type 'C<number>'.
tests/cases/conformance/types/awaited/awaitedVariance.ts(58,1): error TS2322: Type 'C<Promise<number>>' is not assignable to type 'C<number>'.
Types of property 'x' are incompatible.
Type 'number' is not assignable to type '{ _tag: string; } & number'.
Type 'number' is not assignable to type '{ _tag: string; }'.
tests/cases/conformance/types/awaited/awaitedVariance.ts(84,1): error TS2322: Type 'D<Promise<number>>' is not assignable to type 'D<number>'.
tests/cases/conformance/types/awaited/awaitedVariance.ts(68,1): error TS2322: Type 'D<Promise<number>>' is not assignable to type 'D<number>'.
Types of property 'a' are incompatible.
Type 'C<Promise<number>>' is not assignable to type 'C<number>'.

Expand Down Expand Up @@ -30,22 +30,6 @@ tests/cases/conformance/types/awaited/awaitedVariance.ts(84,1): error TS2322: Ty
declare let pl0: PromiseLike<number>;
declare let pl1: PromiseLike<PromiseLike<number>>;
declare let pl2: PromiseLike<awaited number>;
pl0 = pl1;
pl0 = pl2;
pl1 = pl0;
pl1 = pl2;
pl2 = pl0;
pl2 = pl1;

function fn2<T>(pl0: PromiseLike<T>, pl1: PromiseLike<PromiseLike<T>>, pl2: PromiseLike<awaited T>) {
pl0 = pl1;
pl0 = pl2;
pl1 = pl0;
pl1 = pl2;
pl2 = pl0;
pl2 = pl1;
}

pl0 = p0;
pl0 = p1;
pl0 = p2;
Expand Down
30 changes: 0 additions & 30 deletions tests/baselines/reference/awaitedVariance.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,6 @@ function fn1<T>(p0: Promise<T>, p1: Promise<Promise<T>>, p2: Promise<awaited T>)
declare let pl0: PromiseLike<number>;
declare let pl1: PromiseLike<PromiseLike<number>>;
declare let pl2: PromiseLike<awaited number>;
pl0 = pl1;
pl0 = pl2;
pl1 = pl0;
pl1 = pl2;
pl2 = pl0;
pl2 = pl1;

function fn2<T>(pl0: PromiseLike<T>, pl1: PromiseLike<PromiseLike<T>>, pl2: PromiseLike<awaited T>) {
pl0 = pl1;
pl0 = pl2;
pl1 = pl0;
pl1 = pl2;
pl2 = pl0;
pl2 = pl1;
}

pl0 = p0;
pl0 = p1;
pl0 = p2;
Expand Down Expand Up @@ -136,20 +120,6 @@ function fn1(p0, p1, p2) {
p2 = p0;
p2 = p1;
}
pl0 = pl1;
pl0 = pl2;
pl1 = pl0;
pl1 = pl2;
pl2 = pl0;
pl2 = pl1;
function fn2(pl0, pl1, pl2) {
pl0 = pl1;
pl0 = pl2;
pl1 = pl0;
pl1 = pl2;
pl2 = pl0;
pl2 = pl1;
}
pl0 = p0;
pl0 = p1;
pl0 = p2;
Expand Down
Loading