Merge pull request #9056 from Microsoft/async_function_return_type

Contextually type return statement in async function
This commit is contained in:
Andy 2016-06-09 11:34:46 -07:00 committed by GitHub
commit 3954e9956a
5 changed files with 91 additions and 0 deletions

View file

@ -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);
}

View 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];
});
}

View 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];
}

View 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
}

View 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];
}