Skip to content

Commit 31a94fc

Browse files
committed
Cleaning up InferenceContext
1 parent 737867e commit 31a94fc

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

src/compiler/checker.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5715,7 +5715,7 @@ namespace ts {
57155715
function getInferenceMapper(context: InferenceContext): TypeMapper {
57165716
if (!context.mapper) {
57175717
const mapper: TypeMapper = t => {
5718-
const typeParameters = context.typeParameters;
5718+
const typeParameters = context.signature.typeParameters;
57195719
for (let i = 0; i < typeParameters.length; i++) {
57205720
if (t === typeParameters[i]) {
57215721
context.inferences[i].isFixed = true;
@@ -5724,7 +5724,7 @@ namespace ts {
57245724
}
57255725
return t;
57265726
};
5727-
mapper.mappedTypes = context.typeParameters;
5727+
mapper.mappedTypes = context.signature.typeParameters;
57285728
mapper.context = context;
57295729
context.mapper = mapper;
57305730
}
@@ -7508,15 +7508,12 @@ namespace ts {
75087508
}
75097509

75107510
function createInferenceContext(signature: Signature, inferUnionTypes: boolean): InferenceContext {
7511-
const typeParameters = signature.typeParameters;
7512-
const returnType = getReturnTypeOfSignature(signature);
75137511
const inferences = map(signature.typeParameters, createTypeInferencesObject);
75147512
return {
7515-
typeParameters,
7516-
returnType,
7513+
signature,
75177514
inferUnionTypes,
75187515
inferences,
7519-
inferredTypes: new Array(typeParameters.length),
7516+
inferredTypes: new Array(signature.typeParameters.length),
75207517
};
75217518
}
75227519

@@ -7547,6 +7544,7 @@ namespace ts {
75477544
}
75487545

75497546
function inferTypes(context: InferenceContext, source: Type, target: Type) {
7547+
const typeParameters = context.signature.typeParameters;
75507548
let sourceStack: Type[];
75517549
let targetStack: Type[];
75527550
let depth = 0;
@@ -7606,7 +7604,6 @@ namespace ts {
76067604
if (source.flags & TypeFlags.ContainsAnyFunctionType) {
76077605
return;
76087606
}
7609-
const typeParameters = context.typeParameters;
76107607
for (let i = 0; i < typeParameters.length; i++) {
76117608
if (target === typeParameters[i]) {
76127609
const inferences = context.inferences[i];
@@ -7646,7 +7643,7 @@ namespace ts {
76467643
let typeParameter: TypeParameter;
76477644
// First infer to each type in union or intersection that isn't a type parameter
76487645
for (const t of targetTypes) {
7649-
if (t.flags & TypeFlags.TypeParameter && contains(context.typeParameters, t)) {
7646+
if (t.flags & TypeFlags.TypeParameter && contains(typeParameters, t)) {
76507647
typeParameter = <TypeParameter>t;
76517648
typeParameterCount++;
76527649
}
@@ -7805,10 +7802,11 @@ namespace ts {
78057802
// we made at least one inference that wasn't shallow, or
78067803
// the type parameter has a primitive type constraint, or
78077804
// the type parameter wasn't fixed and is referenced at top level in the return type.
7808-
const keepLiteralTypes = !context.inferences[index].shallow ||
7809-
hasPrimitiveConstraint(context.typeParameters[index]) ||
7810-
!context.inferences[index].isFixed && hasTypeParameterAtTopLevel(context.returnType, context.typeParameters[index]);
7811-
const baseInferences = keepLiteralTypes ? inferences : map(inferences, getBaseTypeOfLiteralType);
7805+
const signature = context.signature;
7806+
const widenLiteralTypes = context.inferences[index].shallow &&
7807+
!hasPrimitiveConstraint(signature.typeParameters[index]) &&
7808+
(context.inferences[index].isFixed || !hasTypeParameterAtTopLevel(getReturnTypeOfSignature(signature), signature.typeParameters[index]));
7809+
const baseInferences = widenLiteralTypes ? map(inferences, getBaseTypeOfLiteralType) : inferences;
78127810
// Infer widened union or supertype, or the unknown type for no common supertype
78137811
const unionOrSuperType = context.inferUnionTypes ? getUnionType(baseInferences, /*subtypeReduction*/ true) : getCommonSupertype(baseInferences);
78147812
inferredType = unionOrSuperType ? getWidenedType(unionOrSuperType) : unknownType;
@@ -7826,7 +7824,7 @@ namespace ts {
78267824

78277825
// Only do the constraint check if inference succeeded (to prevent cascading errors)
78287826
if (inferenceSucceeded) {
7829-
const constraint = getConstraintOfTypeParameter(context.typeParameters[index]);
7827+
const constraint = getConstraintOfTypeParameter(context.signature.typeParameters[index]);
78307828
if (constraint) {
78317829
const instantiatedConstraint = instantiateType(constraint, getInferenceMapper(context));
78327830
if (!isTypeAssignableTo(inferredType, getTypeWithThisArgument(instantiatedConstraint, inferredType))) {

src/compiler/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2590,8 +2590,7 @@ namespace ts {
25902590

25912591
/* @internal */
25922592
export interface InferenceContext {
2593-
typeParameters: TypeParameter[]; // Type parameters for which inferences are made
2594-
returnType: Type; // Return type used when determining whether to widen literal types
2593+
signature: Signature; // Generic signature for which inferences are made
25952594
inferUnionTypes: boolean; // Infer union types for disjoint candidates (otherwise undefinedType)
25962595
inferences: TypeInferences[]; // Inferences made for each type parameter
25972596
inferredTypes: Type[]; // Inferred type for each type parameter

0 commit comments

Comments
 (0)