@@ -22403,7 +22403,7 @@ namespace ts {
22403
22403
inferFromTypes((source as IndexedAccessType).objectType, (target as IndexedAccessType).objectType);
22404
22404
inferFromTypes((source as IndexedAccessType).indexType, (target as IndexedAccessType).indexType);
22405
22405
}
22406
- else if (source.flags & TypeFlags.StringMapping && target.flags & TypeFlags.StringMapping) {
22406
+ else if (source. flags & TypeFlags.StringMapping && target.flags & TypeFlags.StringMapping) {
22407
22407
if ((source as StringMappingType).symbol === (target as StringMappingType).symbol) {
22408
22408
inferFromTypes((source as StringMappingType).type, (target as StringMappingType).type);
22409
22409
}
@@ -22943,39 +22943,46 @@ namespace ts {
22943
22943
if (!inference.inferredType) {
22944
22944
let inferredType: Type | undefined;
22945
22945
const signature = context.signature;
22946
- if (signature) {
22947
- const inferredCovariantType = inference.candidates ? getCovariantInference(inference, signature) : undefined;
22948
- if (inference.contraCandidates) {
22949
- // If we have both co- and contra-variant inferences, we prefer the contra-variant inference
22950
- // unless the co-variant inference is a subtype of some contra-variant inference and not 'never'.
22951
- inferredType = inferredCovariantType && !(inferredCovariantType.flags & TypeFlags.Never) &&
22952
- some(inference.contraCandidates, t => isTypeSubtypeOf(inferredCovariantType, t)) ?
22953
- inferredCovariantType : getContravariantInference(inference);
22954
- }
22955
- else if (inferredCovariantType) {
22956
- inferredType = inferredCovariantType;
22957
- }
22958
- else if (context.flags & InferenceFlags.NoDefault) {
22959
- // We use silentNeverType as the wildcard that signals no inferences.
22960
- inferredType = silentNeverType;
22946
+ if (!(inference.priority! & InferencePriority.BindingPattern)) {
22947
+ // Binding pattern inferences provide a contextual type for other inferences,
22948
+ // but cannot stand alone - they are expected to be overwritten by another
22949
+ // inference with higher priority before fixing. This prevents highly suspicious
22950
+ // patterns like `function f<T>(): T; const { foo } = f()` from inferring T as
22951
+ // `{ foo: any }`.
22952
+ if (signature) {
22953
+ const inferredCovariantType = inference.candidates ? getCovariantInference(inference, signature) : undefined;
22954
+ if (inference.contraCandidates) {
22955
+ // If we have both co- and contra-variant inferences, we prefer the contra-variant inference
22956
+ // unless the co-variant inference is a subtype of some contra-variant inference and not 'never'.
22957
+ inferredType = inferredCovariantType && !(inferredCovariantType.flags & TypeFlags.Never) &&
22958
+ some(inference.contraCandidates, t => isTypeSubtypeOf(inferredCovariantType, t)) ?
22959
+ inferredCovariantType : getContravariantInference(inference);
22960
+ }
22961
+ else if (inferredCovariantType) {
22962
+ inferredType = inferredCovariantType;
22963
+ }
22964
+ else if (context.flags & InferenceFlags.NoDefault) {
22965
+ // We use silentNeverType as the wildcard that signals no inferences.
22966
+ inferredType = silentNeverType;
22967
+ }
22968
+ else {
22969
+ // Infer either the default or the empty object type when no inferences were
22970
+ // made. It is important to remember that in this case, inference still
22971
+ // succeeds, meaning there is no error for not having inference candidates. An
22972
+ // inference error only occurs when there are *conflicting* candidates, i.e.
22973
+ // candidates with no common supertype.
22974
+ const defaultType = getDefaultFromTypeParameter(inference.typeParameter);
22975
+ if (defaultType) {
22976
+ // Instantiate the default type. Any forward reference to a type
22977
+ // parameter should be instantiated to the empty object type.
22978
+ inferredType = instantiateType(defaultType, mergeTypeMappers(createBackreferenceMapper(context, index), context.nonFixingMapper));
22979
+ }
22980
+ }
22961
22981
}
22962
22982
else {
22963
- // Infer either the default or the empty object type when no inferences were
22964
- // made. It is important to remember that in this case, inference still
22965
- // succeeds, meaning there is no error for not having inference candidates. An
22966
- // inference error only occurs when there are *conflicting* candidates, i.e.
22967
- // candidates with no common supertype.
22968
- const defaultType = getDefaultFromTypeParameter(inference.typeParameter);
22969
- if (defaultType) {
22970
- // Instantiate the default type. Any forward reference to a type
22971
- // parameter should be instantiated to the empty object type.
22972
- inferredType = instantiateType(defaultType, mergeTypeMappers(createBackreferenceMapper(context, index), context.nonFixingMapper));
22973
- }
22983
+ inferredType = getTypeFromInference(inference);
22974
22984
}
22975
22985
}
22976
- else {
22977
- inferredType = getTypeFromInference(inference);
22978
- }
22979
22986
22980
22987
inference.inferredType = inferredType || getDefaultTypeArgumentType(!!(context.flags & InferenceFlags.AnyDefault));
22981
22988
@@ -29902,7 +29909,8 @@ namespace ts {
29902
29909
// 'let f: (x: string) => number = wrap(s => s.length)', we infer from the declared type of 'f' to the
29903
29910
// return type of 'wrap'.
29904
29911
if (node.kind !== SyntaxKind.Decorator) {
29905
- const contextualType = getContextualType(node, every(signature.typeParameters, p => !!getDefaultFromTypeParameter(p)) ? ContextFlags.SkipBindingPatterns : ContextFlags.None);
29912
+ const skipBindingPatterns = every(signature.typeParameters, p => !!getDefaultFromTypeParameter(p));
29913
+ const contextualType = getContextualType(node, skipBindingPatterns ? ContextFlags.SkipBindingPatterns : ContextFlags.None);
29906
29914
if (contextualType) {
29907
29915
const inferenceTargetType = getReturnTypeOfSignature(signature);
29908
29916
if (couldContainTypeVariables(inferenceTargetType)) {
@@ -29924,7 +29932,9 @@ namespace ts {
29924
29932
getOrCreateTypeFromSignature(getSignatureInstantiationWithoutFillingInTypeArguments(contextualSignature, contextualSignature.typeParameters)) :
29925
29933
instantiatedType;
29926
29934
// Inferences made from return types have lower priority than all other inferences.
29927
- inferTypes(context.inferences, inferenceSourceType, inferenceTargetType, InferencePriority.ReturnType);
29935
+ const isFromBindingPattern = !skipBindingPatterns && getContextualType(node, ContextFlags.SkipBindingPatterns) !== contextualType;
29936
+ const priority = InferencePriority.ReturnType | (isFromBindingPattern ? InferencePriority.BindingPattern : 0);
29937
+ inferTypes(context.inferences, inferenceSourceType, inferenceTargetType, priority);
29928
29938
// Create a type mapper for instantiating generic contextual types using the inferences made
29929
29939
// from the return type. We need a separate inference pass here because (a) instantiation of
29930
29940
// the source type uses the outer context's return mapper (which excludes inferences made from
0 commit comments