From b96f6cd84ca1f920c2c44b899d49d45b24c71be1 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 1 Sep 2016 06:47:18 -0700 Subject: [PATCH] Union type instead of best common supertype for multiple return statements --- src/compiler/checker.ts | 16 ++-------------- src/compiler/diagnosticMessages.json | 8 -------- 2 files changed, 2 insertions(+), 22 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b59e4eae607..e49ab907d71 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -12477,20 +12477,8 @@ namespace ts { return isAsync ? createPromiseReturnType(func, voidType) : voidType; } } - // When yield/return statements are contextually typed we allow the return type to be a union type. - // Otherwise we require the yield/return expressions to have a best common supertype. - type = contextualSignature ? getUnionType(types, /*subtypeReduction*/ true) : getCommonSupertype(types); - if (!type) { - if (funcIsGenerator) { - error(func, Diagnostics.No_best_common_type_exists_among_yield_expressions); - return createIterableIteratorType(unknownType); - } - else { - error(func, Diagnostics.No_best_common_type_exists_among_return_expressions); - // Defer to unioning the return types so we get a) downstream errors earlier and b) better Salsa experience - return isAsync ? createPromiseReturnType(func, getUnionType(types, /*subtypeReduction*/ true)) : getUnionType(types, /*subtypeReduction*/ true); - } - } + // Return a union of the return expression types. + type = getUnionType(types, /*subtypeReduction*/ true); if (funcIsGenerator) { type = createIterableIteratorType(type); diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index b8978b32571..33cd602e5a7 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1067,10 +1067,6 @@ "category": "Error", "code": 2353 }, - "No best common type exists among return expressions.": { - "category": "Error", - "code": 2354 - }, "A function whose declared type is neither 'void' nor 'any' must return a value.": { "category": "Error", "code": 2355 @@ -1635,10 +1631,6 @@ "category": "Error", "code": 2503 }, - "No best common type exists among yield expressions.": { - "category": "Error", - "code": 2504 - }, "A generator cannot have a 'void' type annotation.": { "category": "Error", "code": 2505