Skip to content

Commit 5eb8f71

Browse files
committed
addressed PR feedback
1 parent 71b98e0 commit 5eb8f71

File tree

4 files changed

+85
-6
lines changed

4 files changed

+85
-6
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9811,17 +9811,19 @@ namespace ts {
98119811
return aggregatedTypes;
98129812
}
98139813

9814-
// TypeScript Specification 1.0 (6.3) - July 2014
9815-
// An explicitly typed function whose return type isn't the Void or the Any type
9816-
// must have at least one return statement somewhere in its body.
9817-
// An exception to this rule is if the function implementation consists of a single 'throw' statement.
9818-
// @param returnType - return type of the function, can be undefined if return type is not explicitly specified
9814+
/*
9815+
*TypeScript Specification 1.0 (6.3) - July 2014
9816+
* An explicitly typed function whose return type isn't the Void or the Any type
9817+
* must have at least one return statement somewhere in its body.
9818+
* An exception to this rule is if the function implementation consists of a single 'throw' statement.
9819+
* @param returnType - return type of the function, can be undefined if return type is not explicitly specified
9820+
*/
98199821
function checkAllCodePathsInNonVoidFunctionReturnOrThrow(func: FunctionLikeDeclaration, returnType: Type): void {
98209822
if (!produceDiagnostics) {
98219823
return;
98229824
}
98239825

9824-
// Functions with explicitly specified return type which is either 'void' or 'any' don't need any return expressions.
9826+
// Functions with with an explicitly specified 'void' or 'any' return type don't need any return expressions.
98259827
if (returnType && (returnType === voidType || isTypeAny(returnType))) {
98269828
return;
98279829
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
tests/cases/compiler/reachabilityChecks7.ts(3,16): error TS7030: Not all code paths return a value.
2+
tests/cases/compiler/reachabilityChecks7.ts(6,9): error TS7030: Not all code paths return a value.
3+
4+
5+
==== tests/cases/compiler/reachabilityChecks7.ts (2 errors) ====
6+
7+
// async function without return type annotation - error
8+
async function f1() {
9+
~~
10+
!!! error TS7030: Not all code paths return a value.
11+
}
12+
13+
let x = async function() {
14+
~~~~~
15+
!!! error TS7030: Not all code paths return a value.
16+
}
17+
18+
// async function with which promised type is void - return can be omitted
19+
async function f2(): Promise<void> {
20+
21+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//// [reachabilityChecks7.ts]
2+
3+
// async function without return type annotation - error
4+
async function f1() {
5+
}
6+
7+
let x = async function() {
8+
}
9+
10+
// async function with which promised type is void - return can be omitted
11+
async function f2(): Promise<void> {
12+
13+
}
14+
15+
//// [reachabilityChecks7.js]
16+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promise, generator) {
17+
return new Promise(function (resolve, reject) {
18+
generator = generator.call(thisArg, _arguments);
19+
function cast(value) { return value instanceof Promise && value.constructor === Promise ? value : new Promise(function (resolve) { resolve(value); }); }
20+
function onfulfill(value) { try { step("next", value); } catch (e) { reject(e); } }
21+
function onreject(value) { try { step("throw", value); } catch (e) { reject(e); } }
22+
function step(verb, value) {
23+
var result = generator[verb](value);
24+
result.done ? resolve(result.value) : cast(result.value).then(onfulfill, onreject);
25+
}
26+
step("next", void 0);
27+
});
28+
};
29+
// async function without return type annotation - error
30+
function f1() {
31+
return __awaiter(this, void 0, Promise, function* () {
32+
});
33+
}
34+
let x = function () {
35+
return __awaiter(this, void 0, Promise, function* () {
36+
});
37+
};
38+
// async function with which promised type is void - return can be omitted
39+
function f2() {
40+
return __awaiter(this, void 0, Promise, function* () {
41+
});
42+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @target: ES6
2+
// @noImplicitReturns: true
3+
4+
// async function without return type annotation - error
5+
async function f1() {
6+
}
7+
8+
let x = async function() {
9+
}
10+
11+
// async function with which promised type is void - return can be omitted
12+
async function f2(): Promise<void> {
13+
14+
}

0 commit comments

Comments
 (0)