From 5b24ea80a74dcb73cef336c2ee06957b858c2c89 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 1 Jul 2019 17:46:50 -1000 Subject: [PATCH] Restore union-like behavior for inference to conditional types --- src/compiler/checker.ts | 60 ++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 92df91d0084..893e7901d64 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -15354,36 +15354,11 @@ namespace ts { inferFromTypes(getFalseTypeFromConditionalType(source), getFalseTypeFromConditionalType(target)); } else if (target.flags & TypeFlags.Conditional && !contravariant) { - inferFromTypes(source, getTrueTypeFromConditionalType(target)); - inferFromTypes(source, getFalseTypeFromConditionalType(target)); + const targetTypes = [getTrueTypeFromConditionalType(target), getFalseTypeFromConditionalType(target)]; + inferToMultipleTypes(source, targetTypes, /*isIntersection*/ false); } else if (target.flags & TypeFlags.UnionOrIntersection) { - // We infer from types that are not naked type variables first so that inferences we - // make from nested naked type variables and given slightly higher priority by virtue - // of being first in the candidates array. - let typeVariableCount = 0; - for (const t of (target).types) { - if (getInferenceInfoForType(t)) { - typeVariableCount++; - } - else { - inferFromTypes(source, t); - } - } - // Inferences directly to naked type variables are given lower priority as they are - // less specific. For example, when inferring from Promise to T | Promise, - // we want to infer string for T, not Promise | string. For intersection types - // we only infer to single naked type variables. - if (target.flags & TypeFlags.Union ? typeVariableCount !== 0 : typeVariableCount === 1) { - const savePriority = priority; - priority |= InferencePriority.NakedTypeVariable; - for (const t of (target).types) { - if (getInferenceInfoForType(t)) { - inferFromTypes(source, t); - } - } - priority = savePriority; - } + inferToMultipleTypes(source, (target).types, !!(target.flags & TypeFlags.Intersection)); } else if (source.flags & TypeFlags.Union) { // Source is a union or intersection type, infer from each constituent type @@ -15481,6 +15456,35 @@ namespace ts { return undefined; } + function inferToMultipleTypes(source: Type, targets: Type[], isIntersection: boolean) { + // We infer from types that are not naked type variables first so that inferences we + // make from nested naked type variables and given slightly higher priority by virtue + // of being first in the candidates array. + let typeVariableCount = 0; + for (const t of targets) { + if (getInferenceInfoForType(t)) { + typeVariableCount++; + } + else { + inferFromTypes(source, t); + } + } + // Inferences directly to naked type variables are given lower priority as they are + // less specific. For example, when inferring from Promise to T | Promise, + // we want to infer string for T, not Promise | string. For intersection types + // we only infer to single naked type variables. + if (isIntersection ? typeVariableCount === 1 : typeVariableCount !== 0) { + const savePriority = priority; + priority |= InferencePriority.NakedTypeVariable; + for (const t of targets) { + if (getInferenceInfoForType(t)) { + inferFromTypes(source, t); + } + } + priority = savePriority; + } + } + function inferToMappedType(source: Type, target: MappedType, constraintType: Type): boolean { if (constraintType.flags & TypeFlags.Union) { let result = false;