@@ -15354,36 +15354,11 @@ namespace ts {
15354
15354
inferFromTypes(getFalseTypeFromConditionalType(<ConditionalType>source), getFalseTypeFromConditionalType(<ConditionalType>target));
15355
15355
}
15356
15356
else if (target.flags & TypeFlags.Conditional && !contravariant) {
15357
- inferFromTypes(source, getTrueTypeFromConditionalType (<ConditionalType>target)) ;
15358
- inferFromTypes (source, getFalseTypeFromConditionalType(<ConditionalType>target) );
15357
+ const targetTypes = [getTrueTypeFromConditionalType(<ConditionalType>target), getFalseTypeFromConditionalType (<ConditionalType>target)] ;
15358
+ inferToMultipleTypes (source, targetTypes, /*isIntersection*/ false );
15359
15359
}
15360
15360
else if (target.flags & TypeFlags.UnionOrIntersection) {
15361
- // We infer from types that are not naked type variables first so that inferences we
15362
- // make from nested naked type variables and given slightly higher priority by virtue
15363
- // of being first in the candidates array.
15364
- let typeVariableCount = 0;
15365
- for (const t of (<UnionOrIntersectionType>target).types) {
15366
- if (getInferenceInfoForType(t)) {
15367
- typeVariableCount++;
15368
- }
15369
- else {
15370
- inferFromTypes(source, t);
15371
- }
15372
- }
15373
- // Inferences directly to naked type variables are given lower priority as they are
15374
- // less specific. For example, when inferring from Promise<string> to T | Promise<T>,
15375
- // we want to infer string for T, not Promise<string> | string. For intersection types
15376
- // we only infer to single naked type variables.
15377
- if (target.flags & TypeFlags.Union ? typeVariableCount !== 0 : typeVariableCount === 1) {
15378
- const savePriority = priority;
15379
- priority |= InferencePriority.NakedTypeVariable;
15380
- for (const t of (<UnionOrIntersectionType>target).types) {
15381
- if (getInferenceInfoForType(t)) {
15382
- inferFromTypes(source, t);
15383
- }
15384
- }
15385
- priority = savePriority;
15386
- }
15361
+ inferToMultipleTypes(source, (<UnionOrIntersectionType>target).types, !!(target.flags & TypeFlags.Intersection));
15387
15362
}
15388
15363
else if (source.flags & TypeFlags.Union) {
15389
15364
// Source is a union or intersection type, infer from each constituent type
@@ -15481,6 +15456,35 @@ namespace ts {
15481
15456
return undefined;
15482
15457
}
15483
15458
15459
+ function inferToMultipleTypes(source: Type, targets: Type[], isIntersection: boolean) {
15460
+ // We infer from types that are not naked type variables first so that inferences we
15461
+ // make from nested naked type variables and given slightly higher priority by virtue
15462
+ // of being first in the candidates array.
15463
+ let typeVariableCount = 0;
15464
+ for (const t of targets) {
15465
+ if (getInferenceInfoForType(t)) {
15466
+ typeVariableCount++;
15467
+ }
15468
+ else {
15469
+ inferFromTypes(source, t);
15470
+ }
15471
+ }
15472
+ // Inferences directly to naked type variables are given lower priority as they are
15473
+ // less specific. For example, when inferring from Promise<string> to T | Promise<T>,
15474
+ // we want to infer string for T, not Promise<string> | string. For intersection types
15475
+ // we only infer to single naked type variables.
15476
+ if (isIntersection ? typeVariableCount === 1 : typeVariableCount !== 0) {
15477
+ const savePriority = priority;
15478
+ priority |= InferencePriority.NakedTypeVariable;
15479
+ for (const t of targets) {
15480
+ if (getInferenceInfoForType(t)) {
15481
+ inferFromTypes(source, t);
15482
+ }
15483
+ }
15484
+ priority = savePriority;
15485
+ }
15486
+ }
15487
+
15484
15488
function inferToMappedType(source: Type, target: MappedType, constraintType: Type): boolean {
15485
15489
if (constraintType.flags & TypeFlags.Union) {
15486
15490
let result = false;
0 commit comments