Skip to content

Commit 27c171e

Browse files
committed
Extract covariant inference derivation into function
1 parent 927fe3e commit 27c171e

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/compiler/checker.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11974,30 +11974,34 @@ namespace ts {
1197411974
return inference.priority & InferencePriority.PriorityImpliesCombination ? getIntersectionType(inference.contraCandidates) : getCommonSubtype(inference.contraCandidates);
1197511975
}
1197611976

11977+
function getCovariantInference(inference: InferenceInfo, context: InferenceContext, signature: Signature) {
11978+
// Extract all object literal types and replace them with a single widened and normalized type.
11979+
const candidates = widenObjectLiteralCandidates(inference.candidates);
11980+
// We widen inferred literal types if
11981+
// all inferences were made to top-level ocurrences of the type parameter, and
11982+
// the type parameter has no constraint or its constraint includes no primitive or literal types, and
11983+
// the type parameter was fixed during inference or does not occur at top-level in the return type.
11984+
const widenLiteralTypes = inference.topLevel &&
11985+
!hasPrimitiveConstraint(inference.typeParameter) &&
11986+
(inference.isFixed || !isTypeParameterAtTopLevel(getReturnTypeOfSignature(signature), inference.typeParameter));
11987+
const baseCandidates = widenLiteralTypes ? sameMap(candidates, getWidenedLiteralType) : candidates;
11988+
// If all inferences were made from contravariant positions, infer a common subtype. Otherwise, if
11989+
// union types were requested or if all inferences were made from the return type position, infer a
11990+
// union type. Otherwise, infer a common supertype.
11991+
const unwidenedType = context.flags & InferenceFlags.InferUnionTypes || inference.priority & InferencePriority.PriorityImpliesCombination ?
11992+
getUnionType(baseCandidates, UnionReduction.Subtype) :
11993+
getCommonSupertype(baseCandidates);
11994+
return getWidenedType(unwidenedType);
11995+
}
11996+
1197711997
function getInferredType(context: InferenceContext, index: number): Type {
1197811998
const inference = context.inferences[index];
1197911999
let inferredType = inference.inferredType;
1198012000
if (!inferredType) {
1198112001
const signature = context.signature;
1198212002
if (signature) {
1198312003
if (inference.candidates) {
11984-
// Extract all object literal types and replace them with a single widened and normalized type.
11985-
const candidates = widenObjectLiteralCandidates(inference.candidates);
11986-
// We widen inferred literal types if
11987-
// all inferences were made to top-level ocurrences of the type parameter, and
11988-
// the type parameter has no constraint or its constraint includes no primitive or literal types, and
11989-
// the type parameter was fixed during inference or does not occur at top-level in the return type.
11990-
const widenLiteralTypes = inference.topLevel &&
11991-
!hasPrimitiveConstraint(inference.typeParameter) &&
11992-
(inference.isFixed || !isTypeParameterAtTopLevel(getReturnTypeOfSignature(signature), inference.typeParameter));
11993-
const baseCandidates = widenLiteralTypes ? sameMap(candidates, getWidenedLiteralType) : candidates;
11994-
// If all inferences were made from contravariant positions, infer a common subtype. Otherwise, if
11995-
// union types were requested or if all inferences were made from the return type position, infer a
11996-
// union type. Otherwise, infer a common supertype.
11997-
const unwidenedType = context.flags & InferenceFlags.InferUnionTypes || inference.priority & InferencePriority.PriorityImpliesCombination ?
11998-
getUnionType(baseCandidates, UnionReduction.Subtype) :
11999-
getCommonSupertype(baseCandidates);
12000-
inferredType = getWidenedType(unwidenedType);
12004+
inferredType = getCovariantInference(inference, context, signature);
1200112005
// If we have inferred 'never' but have contravariant candidates. To get a more specific type we
1200212006
// infer from the contravariant candidates instead.
1200312007
if (inferredType.flags & TypeFlags.Never && inference.contraCandidates) {

0 commit comments

Comments
 (0)