Skip to content

Commit ed49c32

Browse files
committed
Weaken the definition of PromiseLike to allow 'T | awaited T'
1 parent 37569d0 commit ed49c32

File tree

7 files changed

+124
-311
lines changed

7 files changed

+124
-311
lines changed

src/lib/es5.d.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,20 +1378,25 @@ declare type PropertyDecorator = (target: Object, propertyKey: string | symbol)
13781378
declare type MethodDecorator = <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
13791379
declare type ParameterDecorator = (target: Object, propertyKey: string | symbol, parameterIndex: number) => void;
13801380

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

1383+
/**
1384+
* A "Promise-like" is an object that has a callable `then` method that accepts fulfillment and rejection handlers.
1385+
* A "Promise-like" implementation may or may not be compatible with native ES2015 or Promise/A+, but provides a similar-
1386+
* enough implementation that a native of Promise/A+ -compatible promise implementation could adopt its result.
1387+
*/
13831388
interface PromiseLike<T> {
13841389
/**
13851390
* Attaches callbacks for the resolution and/or rejection of the Promise.
13861391
* @param onfulfilled The callback to execute when the Promise is resolved.
13871392
* @param onrejected The callback to execute when the Promise is rejected.
13881393
* @returns A Promise for the completion of which ever callback is executed.
13891394
*/
1390-
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>;
1395+
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>;
13911396
}
13921397

13931398
/**
1394-
* Represents the completion of an asynchronous operation
1399+
* Represents the completion of an asynchronous operation.
13951400
*/
13961401
interface Promise<T> {
13971402
/**

tests/baselines/reference/asyncFunctionDeclaration15_es5.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration1
77
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.
88
Construct signature return types 'Thenable' and 'PromiseLike<T>' are incompatible.
99
The types returned by 'then(...)' are incompatible between these types.
10-
Type 'void' is not assignable to type 'PromiseLike<awaited TResult1 | awaited TResult2>'.
10+
Type 'void' is not assignable to type 'PromiseLike<TResult1 | TResult2>'.
1111
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.
1212
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.
1313

@@ -39,7 +39,7 @@ tests/cases/conformance/async/es5/functionDeclarations/asyncFunctionDeclaration1
3939
!!! 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.
4040
!!! error TS1055: Construct signature return types 'Thenable' and 'PromiseLike<T>' are incompatible.
4141
!!! error TS1055: The types returned by 'then(...)' are incompatible between these types.
42-
!!! error TS1055: Type 'void' is not assignable to type 'PromiseLike<awaited TResult1 | awaited TResult2>'.
42+
!!! error TS1055: Type 'void' is not assignable to type 'PromiseLike<TResult1 | TResult2>'.
4343
async function fn7() { return; } // valid: Promise<void>
4444
async function fn8() { return 1; } // valid: Promise<number>
4545
async function fn9() { return null; } // valid: Promise<any>

tests/baselines/reference/awaitedVariance.errors.txt

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
tests/cases/conformance/types/awaited/awaitedVariance.ts(74,1): error TS2322: Type 'C<Promise<number>>' is not assignable to type 'C<number>'.
1+
tests/cases/conformance/types/awaited/awaitedVariance.ts(58,1): error TS2322: Type 'C<Promise<number>>' is not assignable to type 'C<number>'.
22
Types of property 'x' are incompatible.
33
Type 'number' is not assignable to type '{ _tag: string; } & number'.
44
Type 'number' is not assignable to type '{ _tag: string; }'.
5-
tests/cases/conformance/types/awaited/awaitedVariance.ts(84,1): error TS2322: Type 'D<Promise<number>>' is not assignable to type 'D<number>'.
5+
tests/cases/conformance/types/awaited/awaitedVariance.ts(68,1): error TS2322: Type 'D<Promise<number>>' is not assignable to type 'D<number>'.
66
Types of property 'a' are incompatible.
77
Type 'C<Promise<number>>' is not assignable to type 'C<number>'.
88

@@ -30,22 +30,6 @@ tests/cases/conformance/types/awaited/awaitedVariance.ts(84,1): error TS2322: Ty
3030
declare let pl0: PromiseLike<number>;
3131
declare let pl1: PromiseLike<PromiseLike<number>>;
3232
declare let pl2: PromiseLike<awaited number>;
33-
pl0 = pl1;
34-
pl0 = pl2;
35-
pl1 = pl0;
36-
pl1 = pl2;
37-
pl2 = pl0;
38-
pl2 = pl1;
39-
40-
function fn2<T>(pl0: PromiseLike<T>, pl1: PromiseLike<PromiseLike<T>>, pl2: PromiseLike<awaited T>) {
41-
pl0 = pl1;
42-
pl0 = pl2;
43-
pl1 = pl0;
44-
pl1 = pl2;
45-
pl2 = pl0;
46-
pl2 = pl1;
47-
}
48-
4933
pl0 = p0;
5034
pl0 = p1;
5135
pl0 = p2;

tests/baselines/reference/awaitedVariance.js

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,6 @@ function fn1<T>(p0: Promise<T>, p1: Promise<Promise<T>>, p2: Promise<awaited T>)
2121
declare let pl0: PromiseLike<number>;
2222
declare let pl1: PromiseLike<PromiseLike<number>>;
2323
declare let pl2: PromiseLike<awaited number>;
24-
pl0 = pl1;
25-
pl0 = pl2;
26-
pl1 = pl0;
27-
pl1 = pl2;
28-
pl2 = pl0;
29-
pl2 = pl1;
30-
31-
function fn2<T>(pl0: PromiseLike<T>, pl1: PromiseLike<PromiseLike<T>>, pl2: PromiseLike<awaited T>) {
32-
pl0 = pl1;
33-
pl0 = pl2;
34-
pl1 = pl0;
35-
pl1 = pl2;
36-
pl2 = pl0;
37-
pl2 = pl1;
38-
}
39-
4024
pl0 = p0;
4125
pl0 = p1;
4226
pl0 = p2;
@@ -136,20 +120,6 @@ function fn1(p0, p1, p2) {
136120
p2 = p0;
137121
p2 = p1;
138122
}
139-
pl0 = pl1;
140-
pl0 = pl2;
141-
pl1 = pl0;
142-
pl1 = pl2;
143-
pl2 = pl0;
144-
pl2 = pl1;
145-
function fn2(pl0, pl1, pl2) {
146-
pl0 = pl1;
147-
pl0 = pl2;
148-
pl1 = pl0;
149-
pl1 = pl2;
150-
pl2 = pl0;
151-
pl2 = pl1;
152-
}
153123
pl0 = p0;
154124
pl0 = p1;
155125
pl0 = p2;

0 commit comments

Comments
 (0)