From 3206f5fb94d7962c6f0588289a5c670bfc0b4b54 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 23 Jul 2019 06:38:49 -0700 Subject: [PATCH] When inferring from XXX to T | XXX make no inferece for T (instead of never) --- src/compiler/checker.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 0a64e0552e..72d91c184d 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -15515,16 +15515,18 @@ namespace ts { // removing the identically matched constituents. For example, when inferring from // 'string | string[]' to 'string | T' we reduce the types to 'string[]' and 'T'. if (matchingTypes) { - source = removeTypesFromUnionOrIntersection(source, matchingTypes); - target = removeTypesFromUnionOrIntersection(target, matchingTypes); + const s = removeTypesFromUnionOrIntersection(source, matchingTypes); + const t = removeTypesFromUnionOrIntersection(target, matchingTypes); + if (!(s && t)) return; + source = s; + target = t; } } else if (target.flags & TypeFlags.Union && !(target.flags & TypeFlags.EnumLiteral) || target.flags & TypeFlags.Intersection) { const matched = findMatchedType(source, target); if (matched) { inferFromTypes(matched, matched); - source = target.flags & TypeFlags.Union ? neverType : unknownType; - target = removeTypesFromUnionOrIntersection(target, [matched]); + return; } } else if (target.flags & (TypeFlags.IndexedAccess | TypeFlags.Substitution)) { @@ -15993,7 +15995,7 @@ namespace ts { reducedTypes.push(t); } } - return type.flags & TypeFlags.Union ? getUnionType(reducedTypes) : getIntersectionType(reducedTypes); + return reducedTypes.length ? type.flags & TypeFlags.Union ? getUnionType(reducedTypes) : getIntersectionType(reducedTypes) : undefined; } function hasPrimitiveConstraint(type: TypeParameter): boolean {