From 3f5912995b4fb845ff7ec436dcb40ebfe3816b42 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Tue, 21 May 2019 14:38:34 -0700 Subject: [PATCH] Add related span to original declaration on disagreeing variable/property types. --- src/compiler/checker.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e9dddff64af39..9e5c864f6b975 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5352,7 +5352,7 @@ namespace ts { return type; } else if (declaredType !== errorType && type !== errorType && !isTypeIdenticalTo(declaredType, type)) { - errorNextVariableOrPropertyDeclarationMustHaveSameType(declaredType, declaration, type); + errorNextVariableOrPropertyDeclarationMustHaveSameType(/*firstDeclaration*/ undefined, declaredType, declaration, type); } } return declaredType; @@ -26839,7 +26839,7 @@ namespace ts { if (type !== errorType && declarationType !== errorType && !isTypeIdenticalTo(type, declarationType) && !(symbol.flags & SymbolFlags.Assignment)) { - errorNextVariableOrPropertyDeclarationMustHaveSameType(type, node, declarationType); + errorNextVariableOrPropertyDeclarationMustHaveSameType(symbol.valueDeclaration, type, node, declarationType); } if (node.initializer) { checkTypeAssignableToAndOptionallyElaborate(checkExpressionCached(node.initializer), declarationType, node, node.initializer, /*headMessage*/ undefined); @@ -26859,17 +26859,24 @@ namespace ts { } } - function errorNextVariableOrPropertyDeclarationMustHaveSameType(firstType: Type, nextDeclaration: Declaration, nextType: Type): void { + function errorNextVariableOrPropertyDeclarationMustHaveSameType(firstDeclaration: Declaration | undefined, firstType: Type, nextDeclaration: Declaration, nextType: Type): void { const nextDeclarationName = getNameOfDeclaration(nextDeclaration); const message = nextDeclaration.kind === SyntaxKind.PropertyDeclaration || nextDeclaration.kind === SyntaxKind.PropertySignature ? Diagnostics.Subsequent_property_declarations_must_have_the_same_type_Property_0_must_be_of_type_1_but_here_has_type_2 : Diagnostics.Subsequent_variable_declarations_must_have_the_same_type_Variable_0_must_be_of_type_1_but_here_has_type_2; - error( + const declName = declarationNameToString(nextDeclarationName); + const err = error( nextDeclarationName, message, - declarationNameToString(nextDeclarationName), + declName, typeToString(firstType), - typeToString(nextType)); + typeToString(nextType) + ); + if (firstDeclaration) { + addRelatedInfo(err, + createDiagnosticForNode(firstDeclaration, Diagnostics._0_was_also_declared_here, declName) + ); + } } function areDeclarationFlagsIdentical(left: Declaration, right: Declaration) {