Merge pull request #9056 from Microsoft/async_function_return_type
Contextually type return statement in async function
This commit is contained in:
commit
3954e9956a
|
@ -8756,6 +8756,16 @@ namespace ts {
|
|||
|
||||
function getContextualTypeForReturnExpression(node: Expression): Type {
|
||||
const func = getContainingFunction(node);
|
||||
|
||||
if (isAsyncFunctionLike(func)) {
|
||||
const contextualReturnType = getContextualReturnType(func);
|
||||
if (contextualReturnType) {
|
||||
return getPromisedType(contextualReturnType);
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (func && !func.asteriskToken) {
|
||||
return getContextualReturnType(func);
|
||||
}
|
||||
|
|
33
tests/baselines/reference/asyncFunctionReturnType.js
Normal file
33
tests/baselines/reference/asyncFunctionReturnType.js
Normal file
|
@ -0,0 +1,33 @@
|
|||
//// [asyncFunctionReturnType.ts]
|
||||
async function fAsync() {
|
||||
// Without explicit type annotation, this is just an array.
|
||||
return [1, true];
|
||||
}
|
||||
|
||||
async function fAsyncExplicit(): Promise<[number, boolean]> {
|
||||
// This is contextually typed as a tuple.
|
||||
return [1, true];
|
||||
}
|
||||
|
||||
|
||||
//// [asyncFunctionReturnType.js]
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments)).next());
|
||||
});
|
||||
};
|
||||
function fAsync() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
// Without explicit type annotation, this is just an array.
|
||||
return [1, true];
|
||||
});
|
||||
}
|
||||
function fAsyncExplicit() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
// This is contextually typed as a tuple.
|
||||
return [1, true];
|
||||
});
|
||||
}
|
16
tests/baselines/reference/asyncFunctionReturnType.symbols
Normal file
16
tests/baselines/reference/asyncFunctionReturnType.symbols
Normal file
|
@ -0,0 +1,16 @@
|
|||
=== tests/cases/compiler/asyncFunctionReturnType.ts ===
|
||||
async function fAsync() {
|
||||
>fAsync : Symbol(fAsync, Decl(asyncFunctionReturnType.ts, 0, 0))
|
||||
|
||||
// Without explicit type annotation, this is just an array.
|
||||
return [1, true];
|
||||
}
|
||||
|
||||
async function fAsyncExplicit(): Promise<[number, boolean]> {
|
||||
>fAsyncExplicit : Symbol(fAsyncExplicit, Decl(asyncFunctionReturnType.ts, 3, 1))
|
||||
>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, --, --))
|
||||
|
||||
// This is contextually typed as a tuple.
|
||||
return [1, true];
|
||||
}
|
||||
|
22
tests/baselines/reference/asyncFunctionReturnType.types
Normal file
22
tests/baselines/reference/asyncFunctionReturnType.types
Normal file
|
@ -0,0 +1,22 @@
|
|||
=== tests/cases/compiler/asyncFunctionReturnType.ts ===
|
||||
async function fAsync() {
|
||||
>fAsync : () => Promise<(number | boolean)[]>
|
||||
|
||||
// Without explicit type annotation, this is just an array.
|
||||
return [1, true];
|
||||
>[1, true] : (number | boolean)[]
|
||||
>1 : number
|
||||
>true : boolean
|
||||
}
|
||||
|
||||
async function fAsyncExplicit(): Promise<[number, boolean]> {
|
||||
>fAsyncExplicit : () => Promise<[number, boolean]>
|
||||
>Promise : Promise<T>
|
||||
|
||||
// This is contextually typed as a tuple.
|
||||
return [1, true];
|
||||
>[1, true] : [number, boolean]
|
||||
>1 : number
|
||||
>true : boolean
|
||||
}
|
||||
|
10
tests/cases/compiler/asyncFunctionReturnType.ts
Normal file
10
tests/cases/compiler/asyncFunctionReturnType.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
// @target: ES6
|
||||
async function fAsync() {
|
||||
// Without explicit type annotation, this is just an array.
|
||||
return [1, true];
|
||||
}
|
||||
|
||||
async function fAsyncExplicit(): Promise<[number, boolean]> {
|
||||
// This is contextually typed as a tuple.
|
||||
return [1, true];
|
||||
}
|
Loading…
Reference in a new issue