From 129a4f190808ee3bda05fa76e32d1d92d8f667aa Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 5 Mar 2016 16:16:19 -0800 Subject: [PATCH] Check return type includes 'undefined' in function with implicit return --- src/compiler/checker.ts | 3 +++ src/compiler/diagnosticMessages.json | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 6d09cbffce8..f15af886c36 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11095,6 +11095,9 @@ namespace ts { // NOTE: having returnType !== undefined is a precondition for entering this branch so func.type will always be present error(func.type, Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value); } + else if (returnType && strictNullChecks && !isTypeAssignableTo(undefinedType, returnType)) { + error(func.type, Diagnostics.Function_lacks_ending_return_statement_and_return_type_does_not_include_undefined); + } else if (compilerOptions.noImplicitReturns) { if (!returnType) { // If return type annotation is omitted check if function has any explicit return statements. diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 0b70ab98ce7..bd8701d9cea 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1091,6 +1091,10 @@ "category": "Error", "code": 2365 }, + "Function lacks ending return statement and return type does not include 'undefined'.": { + "category": "Error", + "code": 2366 + }, "Type parameter name cannot be '{0}'": { "category": "Error", "code": 2368