Skip to content

Commit 8f7fd09

Browse files
committed
Set inference result to any isntead of {} for .js files if generic type parameter inference found no candidates
1 parent f7242f3 commit 8f7fd09

File tree

5 files changed

+638
-3
lines changed

5 files changed

+638
-3
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9177,13 +9177,14 @@ namespace ts {
91779177
}
91789178
}
91799179

9180-
function createInferenceContext(signature: Signature, inferUnionTypes: boolean): InferenceContext {
9180+
function createInferenceContext(signature: Signature, inferUnionTypes: boolean, useAnyForNoInferences: boolean): InferenceContext {
91819181
const inferences = map(signature.typeParameters, createTypeInferencesObject);
91829182
return {
91839183
signature,
91849184
inferUnionTypes,
91859185
inferences,
91869186
inferredTypes: new Array(signature.typeParameters.length),
9187+
useAnyForNoInferences
91879188
};
91889189
}
91899190

@@ -9613,6 +9614,9 @@ namespace ts {
96139614
context.inferredTypes[index] = inferredType = instantiatedConstraint;
96149615
}
96159616
}
9617+
if (context.useAnyForNoInferences && !inferences.length && inferredType === emptyObjectType) {
9618+
context.inferredTypes[index] = inferredType = anyType;
9619+
}
96169620
}
96179621
else if (context.failedTypeParameterIndex === undefined || context.failedTypeParameterIndex > index) {
96189622
// If inference failed, it is necessary to record the index of the failed type parameter (the one we are on).
@@ -13634,7 +13638,7 @@ namespace ts {
1363413638

1363513639
// Instantiate a generic signature in the context of a non-generic signature (section 3.8.5 in TypeScript spec)
1363613640
function instantiateSignatureInContextOf(signature: Signature, contextualSignature: Signature, contextualMapper: TypeMapper): Signature {
13637-
const context = createInferenceContext(signature, /*inferUnionTypes*/ true);
13641+
const context = createInferenceContext(signature, /*inferUnionTypes*/ true, /*useAnyForNoInferences*/ false);
1363813642
forEachMatchingParameterType(contextualSignature, signature, (source, target) => {
1363913643
// Type parameters from outer context referenced by source type are fixed by instantiation of the source type
1364013644
inferTypesWithContext(context, instantiateType(source, contextualMapper), target);
@@ -14335,7 +14339,7 @@ namespace ts {
1433514339
let candidate: Signature;
1433614340
let typeArgumentsAreValid: boolean;
1433714341
const inferenceContext = originalCandidate.typeParameters
14338-
? createInferenceContext(originalCandidate, /*inferUnionTypes*/ false)
14342+
? createInferenceContext(originalCandidate, /*inferUnionTypes*/ false, /*useAnyForNoInferences*/ isInJavaScriptFile(node))
1433914343
: undefined;
1434014344

1434114345
while (true) {

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3235,6 +3235,7 @@
32353235
mapper?: TypeMapper; // Type mapper for this inference context
32363236
failedTypeParameterIndex?: number; // Index of type parameter for which inference failed
32373237
// It is optional because in contextual signature instantiation, nothing fails
3238+
useAnyForNoInferences?: boolean; // Use any instead of {} for no inferences
32383239
}
32393240

32403241
/* @internal */

0 commit comments

Comments
 (0)