diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a916b898d80..bf36b3abcfb 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11684,7 +11684,10 @@ namespace ts { const hasExplicitReturn = func.flags & NodeFlags.HasExplicitReturn; - if (returnType && !hasExplicitReturn) { + if (returnType === neverType) { + error(func.type, Diagnostics.A_function_returning_never_cannot_have_a_reachable_end_point); + } + else if (returnType && !hasExplicitReturn) { // minimal check: function has syntactic return type annotation and no explicit return statements in the body // this function does not conform to the specification. // NOTE: having returnType !== undefined is a precondition for entering this branch so func.type will always be present @@ -14821,7 +14824,7 @@ namespace ts { if (func) { const signature = getSignatureFromDeclaration(func); const returnType = getReturnTypeOfSignature(signature); - if (strictNullChecks || node.expression) { + if (strictNullChecks || node.expression || returnType === neverType) { const exprType = node.expression ? checkExpressionCached(node.expression) : undefinedType; if (func.asteriskToken) { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 899b4292f04..4c9de081ac5 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1751,6 +1751,10 @@ "category": "Error", "code": 2533 }, + "A function returning 'never' cannot have a reachable end point.": { + "category": "Error", + "code": 2534 + }, "JSX element attributes type '{0}' may not be a union type.": { "category": "Error", "code": 2600