Skip to content

Commit 3954e99

Browse files
author
Andy
authored
Merge pull request #9056 from Microsoft/async_function_return_type
Contextually type return statement in async function
2 parents 75b8293 + 7a2ef42 commit 3954e99

File tree

5 files changed

+91
-0
lines changed

5 files changed

+91
-0
lines changed

src/compiler/checker.ts

+10
Original file line numberDiff line numberDiff line change
@@ -8756,6 +8756,16 @@ namespace ts {
87568756

87578757
function getContextualTypeForReturnExpression(node: Expression): Type {
87588758
const func = getContainingFunction(node);
8759+
8760+
if (isAsyncFunctionLike(func)) {
8761+
const contextualReturnType = getContextualReturnType(func);
8762+
if (contextualReturnType) {
8763+
return getPromisedType(contextualReturnType);
8764+
}
8765+
8766+
return undefined;
8767+
}
8768+
87598769
if (func && !func.asteriskToken) {
87608770
return getContextualReturnType(func);
87618771
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//// [asyncFunctionReturnType.ts]
2+
async function fAsync() {
3+
// Without explicit type annotation, this is just an array.
4+
return [1, true];
5+
}
6+
7+
async function fAsyncExplicit(): Promise<[number, boolean]> {
8+
// This is contextually typed as a tuple.
9+
return [1, true];
10+
}
11+
12+
13+
//// [asyncFunctionReturnType.js]
14+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
15+
return new (P || (P = Promise))(function (resolve, reject) {
16+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17+
function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }
18+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
19+
step((generator = generator.apply(thisArg, _arguments)).next());
20+
});
21+
};
22+
function fAsync() {
23+
return __awaiter(this, void 0, void 0, function* () {
24+
// Without explicit type annotation, this is just an array.
25+
return [1, true];
26+
});
27+
}
28+
function fAsyncExplicit() {
29+
return __awaiter(this, void 0, void 0, function* () {
30+
// This is contextually typed as a tuple.
31+
return [1, true];
32+
});
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/asyncFunctionReturnType.ts ===
2+
async function fAsync() {
3+
>fAsync : Symbol(fAsync, Decl(asyncFunctionReturnType.ts, 0, 0))
4+
5+
// Without explicit type annotation, this is just an array.
6+
return [1, true];
7+
}
8+
9+
async function fAsyncExplicit(): Promise<[number, boolean]> {
10+
>fAsyncExplicit : Symbol(fAsyncExplicit, Decl(asyncFunctionReturnType.ts, 3, 1))
11+
>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))
12+
13+
// This is contextually typed as a tuple.
14+
return [1, true];
15+
}
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/compiler/asyncFunctionReturnType.ts ===
2+
async function fAsync() {
3+
>fAsync : () => Promise<(number | boolean)[]>
4+
5+
// Without explicit type annotation, this is just an array.
6+
return [1, true];
7+
>[1, true] : (number | boolean)[]
8+
>1 : number
9+
>true : boolean
10+
}
11+
12+
async function fAsyncExplicit(): Promise<[number, boolean]> {
13+
>fAsyncExplicit : () => Promise<[number, boolean]>
14+
>Promise : Promise<T>
15+
16+
// This is contextually typed as a tuple.
17+
return [1, true];
18+
>[1, true] : [number, boolean]
19+
>1 : number
20+
>true : boolean
21+
}
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @target: ES6
2+
async function fAsync() {
3+
// Without explicit type annotation, this is just an array.
4+
return [1, true];
5+
}
6+
7+
async function fAsyncExplicit(): Promise<[number, boolean]> {
8+
// This is contextually typed as a tuple.
9+
return [1, true];
10+
}

0 commit comments

Comments
 (0)