Skip to content

Commit ce8c012

Browse files
committed
Add additional test case from the issue comment
1 parent f4b2ec7 commit ce8c012

File tree

3 files changed

+108
-18
lines changed

3 files changed

+108
-18
lines changed
Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,46 @@
11
=== tests/cases/conformance/types/contextualTypes/asyncFunctions/contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts ===
2+
// repro #47682
3+
24
declare class StateMachine<T> {
35
>StateMachine : Symbol(StateMachine, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 0, 0))
4-
>T : Symbol(T, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 0, 27))
6+
>T : Symbol(T, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 2, 27))
57

68
onDone: (a: T) => void;
7-
>onDone : Symbol(StateMachine.onDone, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 0, 31))
8-
>a : Symbol(a, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 1, 11))
9-
>T : Symbol(T, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 0, 27))
9+
>onDone : Symbol(StateMachine.onDone, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 2, 31))
10+
>a : Symbol(a, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 3, 11))
11+
>T : Symbol(T, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 2, 27))
1012
}
1113

1214
declare function createMachine<T>(implementations: {
13-
>createMachine : Symbol(createMachine, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 2, 1))
14-
>T : Symbol(T, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 4, 31))
15-
>implementations : Symbol(implementations, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 4, 34))
15+
>createMachine : Symbol(createMachine, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 4, 1))
16+
>T : Symbol(T, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 6, 31))
17+
>implementations : Symbol(implementations, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 6, 34))
1618

1719
services: Record<string, () => Promise<T> | StateMachine<T>>;
18-
>services : Symbol(services, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 4, 52))
20+
>services : Symbol(services, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 6, 52))
1921
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
2022
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --))
21-
>T : Symbol(T, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 4, 31))
23+
>T : Symbol(T, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 6, 31))
2224
>StateMachine : Symbol(StateMachine, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 0, 0))
23-
>T : Symbol(T, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 4, 31))
25+
>T : Symbol(T, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 6, 31))
2426

2527
}): void;
2628

2729
createMachine<{ count: number }>({
28-
>createMachine : Symbol(createMachine, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 2, 1))
29-
>count : Symbol(count, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 8, 15))
30+
>createMachine : Symbol(createMachine, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 4, 1))
31+
>count : Symbol(count, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 10, 15))
3032

3133
services: {
32-
>services : Symbol(services, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 8, 34))
34+
>services : Symbol(services, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 10, 34))
3335

3436
test: async () => Promise.reject("some err"),
35-
>test : Symbol(test, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 9, 13))
37+
>test : Symbol(test, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 11, 13))
3638
>Promise.reject : Symbol(PromiseConstructor.reject, Decl(lib.es2015.promise.d.ts, --, --))
3739
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --))
3840
>reject : Symbol(PromiseConstructor.reject, Decl(lib.es2015.promise.d.ts, --, --))
3941

4042
async test2() {
41-
>test2 : Symbol(test2, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 10, 49))
43+
>test2 : Symbol(test2, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 12, 49))
4244

4345
return Promise.reject("some err");
4446
>Promise.reject : Symbol(PromiseConstructor.reject, Decl(lib.es2015.promise.d.ts, --, --))
@@ -50,15 +52,49 @@ createMachine<{ count: number }>({
5052
});
5153

5254
function fn1(): () => Promise<{ count: number }> | StateMachine<{ count: number }> {
53-
>fn1 : Symbol(fn1, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 15, 3))
55+
>fn1 : Symbol(fn1, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 17, 3))
5456
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --))
55-
>count : Symbol(count, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 17, 31))
57+
>count : Symbol(count, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 19, 31))
5658
>StateMachine : Symbol(StateMachine, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 0, 0))
57-
>count : Symbol(count, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 17, 65))
59+
>count : Symbol(count, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 19, 65))
5860

5961
return async () => Promise.reject('some err')
6062
>Promise.reject : Symbol(PromiseConstructor.reject, Decl(lib.es2015.promise.d.ts, --, --))
6163
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --))
6264
>reject : Symbol(PromiseConstructor.reject, Decl(lib.es2015.promise.d.ts, --, --))
6365
}
6466

67+
// repro #47682 issuecomment-1174099713
68+
69+
declare function load(): Promise<boolean>;
70+
>load : Symbol(load, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 21, 1))
71+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --))
72+
73+
type LoadCallback = () => Promise<boolean> | string;
74+
>LoadCallback : Symbol(LoadCallback, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 25, 42))
75+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --))
76+
77+
// all of those are essentially the same and should be allowed
78+
const cb1: LoadCallback = async () => load().then(m => m);
79+
>cb1 : Symbol(cb1, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 30, 5))
80+
>LoadCallback : Symbol(LoadCallback, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 25, 42))
81+
>load().then : Symbol(Promise.then, Decl(lib.es5.d.ts, --, --))
82+
>load : Symbol(load, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 21, 1))
83+
>then : Symbol(Promise.then, Decl(lib.es5.d.ts, --, --))
84+
>m : Symbol(m, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 30, 50))
85+
>m : Symbol(m, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 30, 50))
86+
87+
const cb2: LoadCallback = async () => load();
88+
>cb2 : Symbol(cb2, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 31, 5))
89+
>LoadCallback : Symbol(LoadCallback, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 25, 42))
90+
>load : Symbol(load, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 21, 1))
91+
92+
const cb3: LoadCallback = () => load().then(m => m);
93+
>cb3 : Symbol(cb3, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 32, 5))
94+
>LoadCallback : Symbol(LoadCallback, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 25, 42))
95+
>load().then : Symbol(Promise.then, Decl(lib.es5.d.ts, --, --))
96+
>load : Symbol(load, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 21, 1))
97+
>then : Symbol(Promise.then, Decl(lib.es5.d.ts, --, --))
98+
>m : Symbol(m, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 32, 44))
99+
>m : Symbol(m, Decl(contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts, 32, 44))
100+

tests/baselines/reference/contextuallyTypeAsyncFunctionReturnTypeFromUnion.types

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
=== tests/cases/conformance/types/contextualTypes/asyncFunctions/contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts ===
2+
// repro #47682
3+
24
declare class StateMachine<T> {
35
>StateMachine : StateMachine<T>
46

@@ -63,3 +65,42 @@ function fn1(): () => Promise<{ count: number }> | StateMachine<{ count: number
6365
>'some err' : "some err"
6466
}
6567

68+
// repro #47682 issuecomment-1174099713
69+
70+
declare function load(): Promise<boolean>;
71+
>load : () => Promise<boolean>
72+
73+
type LoadCallback = () => Promise<boolean> | string;
74+
>LoadCallback : () => Promise<boolean> | string
75+
76+
// all of those are essentially the same and should be allowed
77+
const cb1: LoadCallback = async () => load().then(m => m);
78+
>cb1 : LoadCallback
79+
>async () => load().then(m => m) : () => Promise<boolean>
80+
>load().then(m => m) : Promise<boolean>
81+
>load().then : <TResult1 = boolean, TResult2 = never>(onfulfilled?: ((value: boolean) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined) => Promise<TResult1 | TResult2>
82+
>load() : Promise<boolean>
83+
>load : () => Promise<boolean>
84+
>then : <TResult1 = boolean, TResult2 = never>(onfulfilled?: ((value: boolean) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined) => Promise<TResult1 | TResult2>
85+
>m => m : (m: boolean) => boolean
86+
>m : boolean
87+
>m : boolean
88+
89+
const cb2: LoadCallback = async () => load();
90+
>cb2 : LoadCallback
91+
>async () => load() : () => Promise<boolean>
92+
>load() : Promise<boolean>
93+
>load : () => Promise<boolean>
94+
95+
const cb3: LoadCallback = () => load().then(m => m);
96+
>cb3 : LoadCallback
97+
>() => load().then(m => m) : () => Promise<boolean>
98+
>load().then(m => m) : Promise<boolean>
99+
>load().then : <TResult1 = boolean, TResult2 = never>(onfulfilled?: ((value: boolean) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined) => Promise<TResult1 | TResult2>
100+
>load() : Promise<boolean>
101+
>load : () => Promise<boolean>
102+
>then : <TResult1 = boolean, TResult2 = never>(onfulfilled?: ((value: boolean) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined) => Promise<TResult1 | TResult2>
103+
>m => m : (m: boolean) => boolean
104+
>m : boolean
105+
>m : boolean
106+

tests/cases/conformance/types/contextualTypes/asyncFunctions/contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// @strict: true
33
// @noEmit: true
44

5+
// repro #47682
6+
57
declare class StateMachine<T> {
68
onDone: (a: T) => void;
79
}
@@ -22,3 +24,14 @@ createMachine<{ count: number }>({
2224
function fn1(): () => Promise<{ count: number }> | StateMachine<{ count: number }> {
2325
return async () => Promise.reject('some err')
2426
}
27+
28+
// repro #47682 issuecomment-1174099713
29+
30+
declare function load(): Promise<boolean>;
31+
32+
type LoadCallback = () => Promise<boolean> | string;
33+
34+
// all of those are essentially the same and should be allowed
35+
const cb1: LoadCallback = async () => load().then(m => m);
36+
const cb2: LoadCallback = async () => load();
37+
const cb3: LoadCallback = () => load().then(m => m);

0 commit comments

Comments
 (0)