Skip to content

Commit 2f229ab

Browse files
authored
Add an extra test for contextually typed return of async function (#52613)
1 parent ff1e08f commit 2f229ab

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed

tests/baselines/reference/asyncFunctionContextuallyTypedReturns.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,25 @@ type MyCallback = (thing: string) => void;
1111
declare function h(cb: (v: boolean) => MyCallback | PromiseLike<MyCallback>): void;
1212
h(v => v ? (abc) => { } : Promise.reject());
1313
h(async v => v ? (def) => { } : Promise.reject());
14+
15+
// repro from #29196
16+
const increment: (
17+
num: number,
18+
str: string
19+
) => Promise<((s: string) => any) | string> | string = async (num, str) => {
20+
return a => {
21+
return a.length
22+
}
23+
}
24+
25+
const increment2: (
26+
num: number,
27+
str: string
28+
) => Promise<((s: string) => any) | string> = async (num, str) => {
29+
return a => {
30+
return a.length
31+
}
32+
}
1433

1534

1635
//// [asyncFunctionContextuallyTypedReturns.js]
@@ -30,3 +49,14 @@ g(v => v ? "contextuallyTypable" : Promise.reject());
3049
g((v) => __awaiter(void 0, void 0, void 0, function* () { return v ? "contextuallyTypable" : Promise.reject(); }));
3150
h(v => v ? (abc) => { } : Promise.reject());
3251
h((v) => __awaiter(void 0, void 0, void 0, function* () { return v ? (def) => { } : Promise.reject(); }));
52+
// repro from #29196
53+
const increment = (num, str) => __awaiter(void 0, void 0, void 0, function* () {
54+
return a => {
55+
return a.length;
56+
};
57+
});
58+
const increment2 = (num, str) => __awaiter(void 0, void 0, void 0, function* () {
59+
return a => {
60+
return a.length;
61+
};
62+
});

tests/baselines/reference/asyncFunctionContextuallyTypedReturns.symbols

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,54 @@ h(async v => v ? (def) => { } : Promise.reject());
7373
>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, --, --))
7474
>reject : Symbol(PromiseConstructor.reject, Decl(lib.es2015.promise.d.ts, --, --))
7575

76+
// repro from #29196
77+
const increment: (
78+
>increment : Symbol(increment, Decl(asyncFunctionContextuallyTypedReturns.ts, 14, 5))
79+
80+
num: number,
81+
>num : Symbol(num, Decl(asyncFunctionContextuallyTypedReturns.ts, 14, 18))
82+
83+
str: string
84+
>str : Symbol(str, Decl(asyncFunctionContextuallyTypedReturns.ts, 15, 14))
85+
86+
) => Promise<((s: string) => any) | string> | string = async (num, str) => {
87+
>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, --, --))
88+
>s : Symbol(s, Decl(asyncFunctionContextuallyTypedReturns.ts, 17, 15))
89+
>num : Symbol(num, Decl(asyncFunctionContextuallyTypedReturns.ts, 17, 62))
90+
>str : Symbol(str, Decl(asyncFunctionContextuallyTypedReturns.ts, 17, 66))
91+
92+
return a => {
93+
>a : Symbol(a, Decl(asyncFunctionContextuallyTypedReturns.ts, 18, 8))
94+
95+
return a.length
96+
>a.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
97+
>a : Symbol(a, Decl(asyncFunctionContextuallyTypedReturns.ts, 18, 8))
98+
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
99+
}
100+
}
101+
102+
const increment2: (
103+
>increment2 : Symbol(increment2, Decl(asyncFunctionContextuallyTypedReturns.ts, 23, 5))
104+
105+
num: number,
106+
>num : Symbol(num, Decl(asyncFunctionContextuallyTypedReturns.ts, 23, 19))
107+
108+
str: string
109+
>str : Symbol(str, Decl(asyncFunctionContextuallyTypedReturns.ts, 24, 14))
110+
111+
) => Promise<((s: string) => any) | string> = async (num, str) => {
112+
>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, --, --))
113+
>s : Symbol(s, Decl(asyncFunctionContextuallyTypedReturns.ts, 26, 15))
114+
>num : Symbol(num, Decl(asyncFunctionContextuallyTypedReturns.ts, 26, 53))
115+
>str : Symbol(str, Decl(asyncFunctionContextuallyTypedReturns.ts, 26, 57))
116+
117+
return a => {
118+
>a : Symbol(a, Decl(asyncFunctionContextuallyTypedReturns.ts, 27, 8))
119+
120+
return a.length
121+
>a.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
122+
>a : Symbol(a, Decl(asyncFunctionContextuallyTypedReturns.ts, 27, 8))
123+
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
124+
}
125+
}
126+

tests/baselines/reference/asyncFunctionContextuallyTypedReturns.types

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,56 @@ h(async v => v ? (def) => { } : Promise.reject());
100100
>Promise : PromiseConstructor
101101
>reject : <T = never>(reason?: any) => Promise<T>
102102

103+
// repro from #29196
104+
const increment: (
105+
>increment : (num: number, str: string) => string | Promise<string | ((s: string) => any)>
106+
107+
num: number,
108+
>num : number
109+
110+
str: string
111+
>str : string
112+
113+
) => Promise<((s: string) => any) | string> | string = async (num, str) => {
114+
>s : string
115+
>async (num, str) => { return a => { return a.length }} : (num: number, str: string) => Promise<(a: string) => number>
116+
>num : number
117+
>str : string
118+
119+
return a => {
120+
>a => { return a.length } : (a: string) => number
121+
>a : string
122+
123+
return a.length
124+
>a.length : number
125+
>a : string
126+
>length : number
127+
}
128+
}
129+
130+
const increment2: (
131+
>increment2 : (num: number, str: string) => Promise<string | ((s: string) => any)>
132+
133+
num: number,
134+
>num : number
135+
136+
str: string
137+
>str : string
138+
139+
) => Promise<((s: string) => any) | string> = async (num, str) => {
140+
>s : string
141+
>async (num, str) => { return a => { return a.length }} : (num: number, str: string) => Promise<(a: string) => number>
142+
>num : number
143+
>str : string
144+
145+
return a => {
146+
>a => { return a.length } : (a: string) => number
147+
>a : string
148+
149+
return a.length
150+
>a.length : number
151+
>a : string
152+
>length : number
153+
}
154+
}
155+

tests/cases/compiler/asyncFunctionContextuallyTypedReturns.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,22 @@ type MyCallback = (thing: string) => void;
1212
declare function h(cb: (v: boolean) => MyCallback | PromiseLike<MyCallback>): void;
1313
h(v => v ? (abc) => { } : Promise.reject());
1414
h(async v => v ? (def) => { } : Promise.reject());
15+
16+
// repro from #29196
17+
const increment: (
18+
num: number,
19+
str: string
20+
) => Promise<((s: string) => any) | string> | string = async (num, str) => {
21+
return a => {
22+
return a.length
23+
}
24+
}
25+
26+
const increment2: (
27+
num: number,
28+
str: string
29+
) => Promise<((s: string) => any) | string> = async (num, str) => {
30+
return a => {
31+
return a.length
32+
}
33+
}

0 commit comments

Comments
 (0)