Skip to content

Commit 0fb415a

Browse files
authored
Merge pull request #14492 from Microsoft/anyInferences
Set inference result to `any` instead of `{}` for .js files if generic type parameter inference found no candidates
2 parents 1bf4f06 + 41dbae5 commit 0fb415a

File tree

5 files changed

+636
-4
lines changed

5 files changed

+636
-4
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9184,13 +9184,14 @@ namespace ts {
91849184
}
91859185
}
91869186

9187-
function createInferenceContext(signature: Signature, inferUnionTypes: boolean): InferenceContext {
9187+
function createInferenceContext(signature: Signature, inferUnionTypes: boolean, useAnyForNoInferences: boolean): InferenceContext {
91889188
const inferences = map(signature.typeParameters, createTypeInferencesObject);
91899189
return {
91909190
signature,
91919191
inferUnionTypes,
91929192
inferences,
91939193
inferredTypes: new Array(signature.typeParameters.length),
9194+
useAnyForNoInferences
91949195
};
91959196
}
91969197

@@ -9604,7 +9605,7 @@ namespace ts {
96049605
getInferenceMapper(context)));
96059606
}
96069607
else {
9607-
inferredType = emptyObjectType;
9608+
inferredType = context.useAnyForNoInferences ? anyType : emptyObjectType;
96089609
}
96099610

96109611
inferenceSucceeded = true;
@@ -13652,7 +13653,7 @@ namespace ts {
1365213653

1365313654
// Instantiate a generic signature in the context of a non-generic signature (section 3.8.5 in TypeScript spec)
1365413655
function instantiateSignatureInContextOf(signature: Signature, contextualSignature: Signature, contextualMapper: TypeMapper): Signature {
13655-
const context = createInferenceContext(signature, /*inferUnionTypes*/ true);
13656+
const context = createInferenceContext(signature, /*inferUnionTypes*/ true, /*useAnyForNoInferences*/ false);
1365613657
forEachMatchingParameterType(contextualSignature, signature, (source, target) => {
1365713658
// Type parameters from outer context referenced by source type are fixed by instantiation of the source type
1365813659
inferTypesWithContext(context, instantiateType(source, contextualMapper), target);
@@ -14353,7 +14354,7 @@ namespace ts {
1435314354
let candidate: Signature;
1435414355
let typeArgumentsAreValid: boolean;
1435514356
const inferenceContext = originalCandidate.typeParameters
14356-
? createInferenceContext(originalCandidate, /*inferUnionTypes*/ false)
14357+
? createInferenceContext(originalCandidate, /*inferUnionTypes*/ false, /*useAnyForNoInferences*/ isInJavaScriptFile(node))
1435714358
: undefined;
1435814359

1435914360
while (true) {

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3235,6 +3235,7 @@ namespace ts {
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)