Report error only on local declaration with additional related information (#49746)

* Add test where the errors are reported in different file
Test for #49739

* Report error only on local declaration with additional related information
Fixes #49739

* Handle existing tests
This commit is contained in:
Sheetal Nandi
2022-07-27 15:31:45 -07:00
committed by GitHub
parent c0461be025
commit 5b0eea48e9
14 changed files with 600 additions and 12 deletions

View File

@@ -39552,8 +39552,9 @@ namespace ts {
}
const indexInfos = getApplicableIndexInfos(type, propNameType);
const interfaceDeclaration = getObjectFlags(type) & ObjectFlags.Interface ? getDeclarationOfKind(type.symbol, SyntaxKind.InterfaceDeclaration) : undefined;
const localPropDeclaration = declaration && declaration.kind === SyntaxKind.BinaryExpression ||
name && name.kind === SyntaxKind.ComputedPropertyName || getParentOfSymbol(prop) === type.symbol ? declaration : undefined;
const propDeclaration = declaration && declaration.kind === SyntaxKind.BinaryExpression ||
name && name.kind === SyntaxKind.ComputedPropertyName ? declaration : undefined;
const localPropDeclaration = getParentOfSymbol(prop) === type.symbol ? declaration : undefined;
for (const info of indexInfos) {
const localIndexDeclaration = info.declaration && getParentOfSymbol(getSymbolOfNode(info.declaration)) === type.symbol ? info.declaration : undefined;
// We check only when (a) the property is declared in the containing type, or (b) the applicable index signature is declared
@@ -39562,8 +39563,12 @@ namespace ts {
const errorNode = localPropDeclaration || localIndexDeclaration ||
(interfaceDeclaration && !some(getBaseTypes(type as InterfaceType), base => !!getPropertyOfObjectType(base, prop.escapedName) && !!getIndexTypeOfType(base, info.keyType)) ? interfaceDeclaration : undefined);
if (errorNode && !isTypeAssignableTo(propType, info.type)) {
error(errorNode, Diagnostics.Property_0_of_type_1_is_not_assignable_to_2_index_type_3,
const diagnostic = createError(errorNode, Diagnostics.Property_0_of_type_1_is_not_assignable_to_2_index_type_3,
symbolToString(prop), typeToString(propType), typeToString(info.keyType), typeToString(info.type));
if (propDeclaration && errorNode !== propDeclaration) {
addRelatedInfo(diagnostic, createDiagnosticForNode(propDeclaration, Diagnostics._0_is_declared_here, symbolToString(prop)));
}
diagnostics.add(diagnostic);
}
}
}