Skip to content

Commit 991d83e

Browse files
committed
Add inference priority level for conditional types in contravariant positions
1 parent 8b83703 commit 991d83e

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17552,9 +17552,12 @@ namespace ts {
1755217552
inferFromTypes(getTrueTypeFromConditionalType(<ConditionalType>source), getTrueTypeFromConditionalType(<ConditionalType>target));
1755317553
inferFromTypes(getFalseTypeFromConditionalType(<ConditionalType>source), getFalseTypeFromConditionalType(<ConditionalType>target));
1755417554
}
17555-
else if (target.flags & TypeFlags.Conditional && !contravariant) {
17555+
else if (target.flags & TypeFlags.Conditional) {
17556+
const savePriority = priority;
17557+
priority |= contravariant ? InferencePriority.ContravariantConditional : 0;
1755617558
const targetTypes = [getTrueTypeFromConditionalType(<ConditionalType>target), getFalseTypeFromConditionalType(<ConditionalType>target)];
1755717559
inferToMultipleTypes(source, targetTypes, target.flags);
17560+
priority = savePriority;
1755817561
}
1755917562
else if (target.flags & TypeFlags.UnionOrIntersection) {
1756017563
inferToMultipleTypes(source, (<UnionOrIntersectionType>target).types, target.flags);

src/compiler/types.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4740,11 +4740,12 @@ namespace ts {
47404740
HomomorphicMappedType = 1 << 1, // Reverse inference for homomorphic mapped type
47414741
PartialHomomorphicMappedType = 1 << 2, // Partial reverse inference for homomorphic mapped type
47424742
MappedTypeConstraint = 1 << 3, // Reverse inference for mapped type
4743-
ReturnType = 1 << 4, // Inference made from return type of generic function
4744-
LiteralKeyof = 1 << 5, // Inference made from a string literal to a keyof T
4745-
NoConstraints = 1 << 6, // Don't infer from constraints of instantiable types
4746-
AlwaysStrict = 1 << 7, // Always use strict rules for contravariant inferences
4747-
MaxValue = 1 << 8, // Seed for inference priority tracking
4743+
ContravariantConditional = 1 << 4, // Conditional type in contravariant position
4744+
ReturnType = 1 << 5, // Inference made from return type of generic function
4745+
LiteralKeyof = 1 << 6, // Inference made from a string literal to a keyof T
4746+
NoConstraints = 1 << 7, // Don't infer from constraints of instantiable types
4747+
AlwaysStrict = 1 << 8, // Always use strict rules for contravariant inferences
4748+
MaxValue = 1 << 9, // Seed for inference priority tracking
47484749

47494750
PriorityImpliesCombination = ReturnType | MappedTypeConstraint | LiteralKeyof, // These priorities imply that the resulting type should be a combination of all candidates
47504751
Circularity = -1, // Inference circularity (value less than all other priorities)

0 commit comments

Comments
 (0)