diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 53e9cc720ce..1a0440be2c7 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -9811,17 +9811,19 @@ namespace ts { return aggregatedTypes; } - // TypeScript Specification 1.0 (6.3) - July 2014 - // An explicitly typed function whose return type isn't the Void or the Any type - // must have at least one return statement somewhere in its body. - // An exception to this rule is if the function implementation consists of a single 'throw' statement. - // @param returnType - return type of the function, can be undefined if return type is not explicitly specified + /* + *TypeScript Specification 1.0 (6.3) - July 2014 + * An explicitly typed function whose return type isn't the Void or the Any type + * must have at least one return statement somewhere in its body. + * An exception to this rule is if the function implementation consists of a single 'throw' statement. + * @param returnType - return type of the function, can be undefined if return type is not explicitly specified + */ function checkAllCodePathsInNonVoidFunctionReturnOrThrow(func: FunctionLikeDeclaration, returnType: Type): void { if (!produceDiagnostics) { return; } - // Functions with explicitly specified return type which is either 'void' or 'any' don't need any return expressions. + // Functions with with an explicitly specified 'void' or 'any' return type don't need any return expressions. if (returnType && (returnType === voidType || isTypeAny(returnType))) { return; } diff --git a/tests/baselines/reference/reachabilityChecks7.errors.txt b/tests/baselines/reference/reachabilityChecks7.errors.txt new file mode 100644 index 00000000000..9e8f803f957 --- /dev/null +++ b/tests/baselines/reference/reachabilityChecks7.errors.txt @@ -0,0 +1,21 @@ +tests/cases/compiler/reachabilityChecks7.ts(3,16): error TS7030: Not all code paths return a value. +tests/cases/compiler/reachabilityChecks7.ts(6,9): error TS7030: Not all code paths return a value. + + +==== tests/cases/compiler/reachabilityChecks7.ts (2 errors) ==== + + // async function without return type annotation - error + async function f1() { + ~~ +!!! error TS7030: Not all code paths return a value. + } + + let x = async function() { + ~~~~~ +!!! error TS7030: Not all code paths return a value. + } + + // async function with which promised type is void - return can be omitted + async function f2(): Promise { + + } \ No newline at end of file diff --git a/tests/baselines/reference/reachabilityChecks7.js b/tests/baselines/reference/reachabilityChecks7.js new file mode 100644 index 00000000000..f9579d5828d --- /dev/null +++ b/tests/baselines/reference/reachabilityChecks7.js @@ -0,0 +1,42 @@ +//// [reachabilityChecks7.ts] + +// async function without return type annotation - error +async function f1() { +} + +let x = async function() { +} + +// async function with which promised type is void - return can be omitted +async function f2(): Promise { + +} + +//// [reachabilityChecks7.js] +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promise, generator) { + return new Promise(function (resolve, reject) { + generator = generator.call(thisArg, _arguments); + function cast(value) { return value instanceof Promise && value.constructor === Promise ? value : new Promise(function (resolve) { resolve(value); }); } + function onfulfill(value) { try { step("next", value); } catch (e) { reject(e); } } + function onreject(value) { try { step("throw", value); } catch (e) { reject(e); } } + function step(verb, value) { + var result = generator[verb](value); + result.done ? resolve(result.value) : cast(result.value).then(onfulfill, onreject); + } + step("next", void 0); + }); +}; +// async function without return type annotation - error +function f1() { + return __awaiter(this, void 0, Promise, function* () { + }); +} +let x = function () { + return __awaiter(this, void 0, Promise, function* () { + }); +}; +// async function with which promised type is void - return can be omitted +function f2() { + return __awaiter(this, void 0, Promise, function* () { + }); +} diff --git a/tests/cases/compiler/reachabilityChecks7.ts b/tests/cases/compiler/reachabilityChecks7.ts new file mode 100644 index 00000000000..11febb320d6 --- /dev/null +++ b/tests/cases/compiler/reachabilityChecks7.ts @@ -0,0 +1,14 @@ +// @target: ES6 +// @noImplicitReturns: true + +// async function without return type annotation - error +async function f1() { +} + +let x = async function() { +} + +// async function with which promised type is void - return can be omitted +async function f2(): Promise { + +} \ No newline at end of file