Skip to content

Commit 529ed2d

Browse files
committed
Stop inferring unions for disjoint callback parameter inferences
1 parent d066e1e commit 529ed2d

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13658,7 +13658,7 @@ namespace ts {
1365813658
return inference.priority! & InferencePriority.PriorityImpliesCombination ? getIntersectionType(inference.contraCandidates!) : getCommonSubtype(inference.contraCandidates!);
1365913659
}
1366013660

13661-
function getCovariantInference(inference: InferenceInfo, context: InferenceContext, signature: Signature) {
13661+
function getCovariantInference(inference: InferenceInfo, signature: Signature) {
1366213662
// Extract all object literal types and replace them with a single widened and normalized type.
1366313663
const candidates = widenObjectLiteralCandidates(inference.candidates!);
1366413664
// We widen inferred literal types if
@@ -13671,10 +13671,9 @@ namespace ts {
1367113671
const baseCandidates = primitiveConstraint ? sameMap(candidates, getRegularTypeOfLiteralType) :
1367213672
widenLiteralTypes ? sameMap(candidates, getWidenedLiteralType) :
1367313673
candidates;
13674-
// If all inferences were made from contravariant positions, infer a common subtype. Otherwise, if
13675-
// union types were requested or if all inferences were made from the return type position, infer a
13676-
// union type. Otherwise, infer a common supertype.
13677-
const unwidenedType = context.flags & InferenceFlags.InferUnionTypes || inference.priority! & InferencePriority.PriorityImpliesCombination ?
13674+
// If all inferences were made from a position that implies a combined result, infer a union type.
13675+
// Otherwise, infer a common supertype.
13676+
const unwidenedType = inference.priority! & InferencePriority.PriorityImpliesCombination ?
1367813677
getUnionType(baseCandidates, UnionReduction.Subtype) :
1367913678
getCommonSupertype(baseCandidates);
1368013679
return getWidenedType(unwidenedType);
@@ -13694,7 +13693,7 @@ namespace ts {
1369413693
inference.contraCandidates = undefined;
1369513694
}
1369613695
if (inference.candidates) {
13697-
inferredType = getCovariantInference(inference, context, signature);
13696+
inferredType = getCovariantInference(inference, signature);
1369813697
}
1369913698
else if (context.flags & InferenceFlags.NoDefault) {
1370013699
// We use silentNeverType as the wildcard that signals no inferences.
@@ -18633,7 +18632,7 @@ namespace ts {
1863318632

1863418633
// Instantiate a generic signature in the context of a non-generic signature (section 3.8.5 in TypeScript spec)
1863518634
function instantiateSignatureInContextOf(signature: Signature, contextualSignature: Signature, contextualMapper?: TypeMapper, compareTypes?: TypeComparer): Signature {
18636-
const context = createInferenceContext(signature.typeParameters!, signature, InferenceFlags.InferUnionTypes, compareTypes);
18635+
const context = createInferenceContext(signature.typeParameters!, signature, InferenceFlags.None, compareTypes);
1863718636
const sourceSignature = contextualMapper ? instantiateSignature(contextualSignature, contextualMapper) : contextualSignature;
1863818637
forEachMatchingParameterType(sourceSignature, signature, (source, target) => {
1863918638
// Type parameters from outer context referenced by source type are fixed by instantiation of the source type

src/compiler/types.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4175,9 +4175,8 @@ namespace ts {
41754175
/* @internal */
41764176
export const enum InferenceFlags {
41774177
None = 0, // No special inference behaviors
4178-
InferUnionTypes = 1 << 0, // Infer union types for disjoint candidates (otherwise unknownType)
4179-
NoDefault = 1 << 1, // Infer unknownType for no inferences (otherwise anyType or emptyObjectType)
4180-
AnyDefault = 1 << 2, // Infer anyType for no inferences (otherwise emptyObjectType)
4178+
NoDefault = 1 << 0, // Infer unknownType for no inferences (otherwise anyType or emptyObjectType)
4179+
AnyDefault = 1 << 1, // Infer anyType for no inferences (otherwise emptyObjectType)
41814180
}
41824181

41834182
/**

0 commit comments

Comments
 (0)