Skip to content

Commit d7be61a

Browse files
author
Andy
authored
Subsequent variable declarations must have same type: Mention location of other declaration (#19356)
* Subsequent variable declarations must have same type: Mention location of other declaration * Fix naming, remove template literal
1 parent 1ea1254 commit d7be61a

File tree

57 files changed

+281
-267
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+281
-267
lines changed

src/compiler/checker.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -560,10 +560,10 @@ namespace ts {
560560
return emitResolver;
561561
}
562562

563-
function error(location: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number): void {
563+
function error(location: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number): void {
564564
const diagnostic = location
565-
? createDiagnosticForNode(location, message, arg0, arg1, arg2)
566-
: createCompilerDiagnostic(message, arg0, arg1, arg2);
565+
? createDiagnosticForNode(location, message, arg0, arg1, arg2, arg3)
566+
: createCompilerDiagnostic(message, arg0, arg1, arg2, arg3);
567567
diagnostics.add(diagnostic);
568568
}
569569

@@ -4417,8 +4417,7 @@ namespace ts {
44174417
jsDocType = declarationType;
44184418
}
44194419
else if (jsDocType !== unknownType && declarationType !== unknownType && !isTypeIdenticalTo(jsDocType, declarationType)) {
4420-
const name = getNameOfDeclaration(declaration);
4421-
error(name, Diagnostics.Subsequent_variable_declarations_must_have_the_same_type_Variable_0_must_be_of_type_1_but_here_has_type_2, declarationNameToString(name), typeToString(jsDocType), typeToString(declarationType));
4420+
errorNextVariableDeclarationMustHaveSameType(symbol.valueDeclaration, jsDocType, declaration, declarationType);
44224421
}
44234422
}
44244423
else if (!jsDocType) {
@@ -20779,7 +20778,7 @@ namespace ts {
2077920778
// initializer is consistent with type associated with the node
2078020779
const declarationType = convertAutoToAny(getWidenedTypeForVariableLikeDeclaration(node));
2078120780
if (type !== unknownType && declarationType !== unknownType && !isTypeIdenticalTo(type, declarationType)) {
20782-
error(node.name, Diagnostics.Subsequent_variable_declarations_must_have_the_same_type_Variable_0_must_be_of_type_1_but_here_has_type_2, declarationNameToString(node.name), typeToString(type), typeToString(declarationType));
20781+
errorNextVariableDeclarationMustHaveSameType(symbol.valueDeclaration, type, node, declarationType);
2078320782
}
2078420783
if (node.initializer) {
2078520784
checkTypeAssignableTo(checkExpressionCached(node.initializer), declarationType, node, /*headMessage*/ undefined);
@@ -20803,6 +20802,21 @@ namespace ts {
2080320802
}
2080420803
}
2080520804

20805+
function errorNextVariableDeclarationMustHaveSameType(firstDeclaration: Declaration, firstType: Type, nextDeclaration: Declaration, nextType: Type): void {
20806+
const firstSourceFile = getSourceFileOfNode(firstDeclaration);
20807+
const firstSpan = getErrorSpanForNode(firstSourceFile, getNameOfDeclaration(firstDeclaration) || firstDeclaration);
20808+
const firstLocation = getLineAndCharacterOfPosition(firstSourceFile, firstSpan.start);
20809+
const firstLocationDescription = firstSourceFile.fileName + " " + firstLocation.line + ":" + firstLocation.character;
20810+
const nextDeclarationName = getNameOfDeclaration(nextDeclaration);
20811+
error(
20812+
nextDeclarationName,
20813+
Diagnostics.Subsequent_variable_declarations_must_have_the_same_type_Variable_0_has_type_1_at_2_but_here_has_type_3,
20814+
declarationNameToString(nextDeclarationName),
20815+
typeToString(firstType),
20816+
firstLocationDescription,
20817+
typeToString(nextType));
20818+
}
20819+
2080620820
function areDeclarationFlagsIdentical(left: Declaration, right: Declaration) {
2080720821
if ((left.kind === SyntaxKind.Parameter && right.kind === SyntaxKind.VariableDeclaration) ||
2080820822
(left.kind === SyntaxKind.VariableDeclaration && right.kind === SyntaxKind.Parameter)) {

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,7 @@
13161316
"category": "Error",
13171317
"code": 2402
13181318
},
1319-
"Subsequent variable declarations must have the same type. Variable '{0}' must be of type '{1}', but here has type '{2}'.": {
1319+
"Subsequent variable declarations must have the same type. Variable '{0}' has type '{1}' at {2}, but here has type '{3}'.": {
13201320
"category": "Error",
13211321
"code": 2403
13221322
},

src/compiler/utilities.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -582,14 +582,14 @@ namespace ts {
582582
}
583583
}
584584

585-
export function createDiagnosticForNode(node: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number): Diagnostic {
585+
export function createDiagnosticForNode(node: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number): Diagnostic {
586586
const sourceFile = getSourceFileOfNode(node);
587-
return createDiagnosticForNodeInSourceFile(sourceFile, node, message, arg0, arg1, arg2);
587+
return createDiagnosticForNodeInSourceFile(sourceFile, node, message, arg0, arg1, arg2, arg3);
588588
}
589589

590-
export function createDiagnosticForNodeInSourceFile(sourceFile: SourceFile, node: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number): Diagnostic {
590+
export function createDiagnosticForNodeInSourceFile(sourceFile: SourceFile, node: Node, message: DiagnosticMessage, arg0?: string | number, arg1?: string | number, arg2?: string | number, arg3?: string | number): Diagnostic {
591591
const span = getErrorSpanForNode(sourceFile, node);
592-
return createFileDiagnostic(sourceFile, span.start, span.length, message, arg0, arg1, arg2);
592+
return createFileDiagnostic(sourceFile, span.start, span.length, message, arg0, arg1, arg2, arg3);
593593
}
594594

595595
export function createDiagnosticForNodeFromMessageChain(node: Node, messageChain: DiagnosticMessageChain): Diagnostic {

tests/baselines/reference/ES5For-of7.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/conformance/statements/for-ofStatements/ES5For-of7.ts(6,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type 'any', but here has type 'any[]'.
1+
tests/cases/conformance/statements/for-ofStatements/ES5For-of7.ts(6,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'x' has type 'any' at tests/cases/conformance/statements/for-ofStatements/ES5For-of7.ts 1:8, but here has type 'any[]'.
22

33

44
==== tests/cases/conformance/statements/for-ofStatements/ES5For-of7.ts (1 errors) ====
@@ -9,5 +9,5 @@ tests/cases/conformance/statements/for-ofStatements/ES5For-of7.ts(6,9): error TS
99
for (var v of []) {
1010
var x = [w, v];
1111
~
12-
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type 'any', but here has type 'any[]'.
12+
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'x' has type 'any' at tests/cases/conformance/statements/for-ofStatements/ES5For-of7.ts 1:8, but here has type 'any[]'.
1313
}

tests/baselines/reference/FunctionAndModuleWithSameNameAndCommonRoot.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
tests/cases/conformance/internalModules/DeclarationMerging/module.ts(2,19): error TS2433: A namespace declaration cannot be in a different file from a class or function with which it is merged.
2-
tests/cases/conformance/internalModules/DeclarationMerging/simple.ts(13,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'fn' must be of type '() => { x: number; y: number; }', but here has type 'typeof Point'.
3-
tests/cases/conformance/internalModules/DeclarationMerging/test.ts(2,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'fn' must be of type '() => { x: number; y: number; }', but here has type 'typeof Point'.
2+
tests/cases/conformance/internalModules/DeclarationMerging/simple.ts(13,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'fn' has type '() => { x: number; y: number; }' at tests/cases/conformance/internalModules/DeclarationMerging/test.ts 0:4, but here has type 'typeof Point'.
3+
tests/cases/conformance/internalModules/DeclarationMerging/test.ts(2,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'fn' has type '() => { x: number; y: number; }' at tests/cases/conformance/internalModules/DeclarationMerging/test.ts 0:4, but here has type 'typeof Point'.
44

55

66
==== tests/cases/conformance/internalModules/DeclarationMerging/function.ts (0 errors) ====
@@ -23,7 +23,7 @@ tests/cases/conformance/internalModules/DeclarationMerging/test.ts(2,5): error T
2323
var fn: () => { x: number; y: number };
2424
var fn = A.Point;
2525
~~
26-
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'fn' must be of type '() => { x: number; y: number; }', but here has type 'typeof Point'.
26+
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'fn' has type '() => { x: number; y: number; }' at tests/cases/conformance/internalModules/DeclarationMerging/test.ts 0:4, but here has type 'typeof Point'.
2727

2828
var cl: { x: number; y: number; }
2929
var cl = A.Point();
@@ -45,7 +45,7 @@ tests/cases/conformance/internalModules/DeclarationMerging/test.ts(2,5): error T
4545
var fn: () => { x: number; y: number };
4646
var fn = B.Point; // not expected to be an error. bug 840000: [corelang] Function of fundule not assignalbe as expected
4747
~~
48-
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'fn' must be of type '() => { x: number; y: number; }', but here has type 'typeof Point'.
48+
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'fn' has type '() => { x: number; y: number; }' at tests/cases/conformance/internalModules/DeclarationMerging/test.ts 0:4, but here has type 'typeof Point'.
4949

5050
var cl: { x: number; y: number; }
5151
var cl = B.Point();

tests/baselines/reference/asyncArrowFunction5_es2017.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(1,11): error TS2304: Cannot find name 'async'.
22
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(1,18): error TS2304: Cannot find name 'await'.
33
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(1,24): error TS1005: ',' expected.
4-
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(1,26): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'void'.
4+
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(1,26): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'void'.
55
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(1,33): error TS1005: '=' expected.
66
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(1,40): error TS1109: Expression expected.
77

@@ -15,7 +15,7 @@ tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es20
1515
~
1616
!!! error TS1005: ',' expected.
1717
~~~~~~~
18-
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'void'.
18+
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'void'.
1919
~
2020
!!! error TS1005: '=' expected.
2121
~~

tests/baselines/reference/asyncArrowFunction5_es5.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(1,11): error TS2304: Cannot find name 'async'.
22
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(1,18): error TS2304: Cannot find name 'await'.
33
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(1,24): error TS1005: ',' expected.
4-
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(1,26): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'void'.
4+
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(1,26): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'void'.
55
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(1,33): error TS1005: '=' expected.
66
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(1,40): error TS1109: Expression expected.
77

@@ -15,7 +15,7 @@ tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(
1515
~
1616
!!! error TS1005: ',' expected.
1717
~~~~~~~
18-
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'void'.
18+
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'void'.
1919
~
2020
!!! error TS1005: '=' expected.
2121
~~

tests/baselines/reference/asyncArrowFunction5_es6.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
tests/cases/conformance/async/es6/asyncArrowFunction/asyncArrowFunction5_es6.ts(1,11): error TS2304: Cannot find name 'async'.
22
tests/cases/conformance/async/es6/asyncArrowFunction/asyncArrowFunction5_es6.ts(1,18): error TS2304: Cannot find name 'await'.
33
tests/cases/conformance/async/es6/asyncArrowFunction/asyncArrowFunction5_es6.ts(1,24): error TS1005: ',' expected.
4-
tests/cases/conformance/async/es6/asyncArrowFunction/asyncArrowFunction5_es6.ts(1,26): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'void'.
4+
tests/cases/conformance/async/es6/asyncArrowFunction/asyncArrowFunction5_es6.ts(1,26): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'void'.
55
tests/cases/conformance/async/es6/asyncArrowFunction/asyncArrowFunction5_es6.ts(1,33): error TS1005: '=' expected.
66
tests/cases/conformance/async/es6/asyncArrowFunction/asyncArrowFunction5_es6.ts(1,40): error TS1109: Expression expected.
77

@@ -15,7 +15,7 @@ tests/cases/conformance/async/es6/asyncArrowFunction/asyncArrowFunction5_es6.ts(
1515
~
1616
!!! error TS1005: ',' expected.
1717
~~~~~~~
18-
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'void'.
18+
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'void'.
1919
~
2020
!!! error TS1005: '=' expected.
2121
~~

tests/baselines/reference/asyncArrowFunction9_es2017.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es2017.ts(1,11): error TS2304: Cannot find name 'async'.
22
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es2017.ts(1,18): error TS2304: Cannot find name 'a'.
33
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es2017.ts(1,37): error TS1005: ',' expected.
4-
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es2017.ts(1,39): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'void'.
4+
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es2017.ts(1,39): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'void'.
55
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es2017.ts(1,46): error TS1005: '=' expected.
66
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es2017.ts(1,53): error TS1109: Expression expected.
77

@@ -15,7 +15,7 @@ tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es20
1515
~
1616
!!! error TS1005: ',' expected.
1717
~~~~~~~
18-
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'void'.
18+
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'void'.
1919
~
2020
!!! error TS1005: '=' expected.
2121
~~

tests/baselines/reference/asyncArrowFunction9_es5.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction9_es5.ts(1,11): error TS2304: Cannot find name 'async'.
22
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction9_es5.ts(1,18): error TS2304: Cannot find name 'a'.
33
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction9_es5.ts(1,37): error TS1005: ',' expected.
4-
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction9_es5.ts(1,39): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'void'.
4+
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction9_es5.ts(1,39): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'void'.
55
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction9_es5.ts(1,46): error TS1005: '=' expected.
66
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction9_es5.ts(1,53): error TS1109: Expression expected.
77

@@ -15,7 +15,7 @@ tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction9_es5.ts(
1515
~
1616
!!! error TS1005: ',' expected.
1717
~~~~~~~
18-
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'void'.
18+
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'void'.
1919
~
2020
!!! error TS1005: '=' expected.
2121
~~

0 commit comments

Comments
 (0)