Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
33 changes: 33 additions & 0 deletions tests/baselines/reference/asyncFunctionReturnType.js
Original file line number Diff line number Diff line change
@@ -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 changes: 16 additions & 0 deletions tests/baselines/reference/asyncFunctionReturnType.symbols
Original file line number Diff line number Diff line change
@@ -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 changes: 22 additions & 0 deletions tests/baselines/reference/asyncFunctionReturnType.types
Original file line number Diff line number Diff line change
@@ -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 changes: 10 additions & 0 deletions tests/cases/compiler/asyncFunctionReturnType.ts
Original file line number Diff line number Diff line change
@@ -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];
}