@@ -4862,17 +4862,8 @@ namespace ts {
4862
4862
function getTypeForBindingElement(declaration: BindingElement): Type | undefined {
4863
4863
const pattern = declaration.parent;
4864
4864
let parentType = getTypeForBindingElementParent(pattern.parent);
4865
- // If parent has the unknown (error) type, then so does this binding element
4866
- if (parentType === errorType) {
4867
- return errorType;
4868
- }
4869
- // If no type was specified or inferred for parent,
4870
- // infer from the initializer of the binding element if one is present.
4871
- // Otherwise, go with the undefined type of the parent.
4872
- if (!parentType) {
4873
- return declaration.initializer ? checkDeclarationInitializer(declaration) : parentType;
4874
- }
4875
- if (isTypeAny(parentType)) {
4865
+ // If no type or an any type was inferred for parent, infer that for the binding element
4866
+ if (!parentType || isTypeAny(parentType)) {
4876
4867
return parentType;
4877
4868
}
4878
4869
// Relax null check on ambient destructuring parameters, since the parameters have no implementation and are just documentation
@@ -5041,8 +5032,11 @@ namespace ts {
5041
5032
}
5042
5033
}
5043
5034
5044
- // Use the type of the initializer expression if one is present
5045
- if (declaration.initializer) {
5035
+ const isParameterOfContextuallyTypedFunction = declaration.kind === SyntaxKind.Parameter && getContextualType(<Expression>declaration.parent);
5036
+
5037
+ // Use the type of the initializer expression if one is present and the declaration is
5038
+ // not a parameter of a contextually typed function
5039
+ if (declaration.initializer && !isParameterOfContextuallyTypedFunction) {
5046
5040
const type = checkDeclarationInitializer(declaration);
5047
5041
return addOptionality(type, isOptional);
5048
5042
}
@@ -5053,8 +5047,9 @@ namespace ts {
5053
5047
return trueType;
5054
5048
}
5055
5049
5056
- // If the declaration specifies a binding pattern, use the type implied by the binding pattern
5057
- if (isBindingPattern(declaration.name)) {
5050
+ // If the declaration specifies a binding pattern and is not a parameter of a contextually
5051
+ // typed function, use the type implied by the binding pattern
5052
+ if (isBindingPattern(declaration.name) && !isParameterOfContextuallyTypedFunction) {
5058
5053
return getTypeFromBindingPattern(declaration.name, /*includePatternInType*/ false, /*reportErrors*/ true);
5059
5054
}
5060
5055
@@ -25638,13 +25633,13 @@ namespace ts {
25638
25633
const parent = node.parent.parent;
25639
25634
const parentType = getTypeForBindingElementParent(parent);
25640
25635
const name = node.propertyName || node.name;
25641
- if (!isBindingPattern(name)) {
25636
+ if (parentType && !isBindingPattern(name)) {
25642
25637
const nameText = getTextOfPropertyName(name);
25643
25638
if (nameText) {
25644
- const property = getPropertyOfType(parentType! , nameText); // TODO: GH#18217
25639
+ const property = getPropertyOfType(parentType, nameText);
25645
25640
if (property) {
25646
25641
markPropertyAsReferenced(property, /*nodeForCheckWriteOnly*/ undefined, /*isThisAccess*/ false); // A destructuring is never a write-only reference.
25647
- checkPropertyAccessibility(parent, !!parent.initializer && parent.initializer.kind === SyntaxKind.SuperKeyword, parentType! , property);
25642
+ checkPropertyAccessibility(parent, !!parent.initializer && parent.initializer.kind === SyntaxKind.SuperKeyword, parentType, property);
25648
25643
}
25649
25644
}
25650
25645
}
0 commit comments