Skip to content

Commit 66aa9e7

Browse files
authored
fix(36247): disallow 'constructor' as a parameter property name (#37285)
1 parent 878f447 commit 66aa9e7

7 files changed

+49
-0
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28912,6 +28912,9 @@ namespace ts {
2891228912
if (!(func.kind === SyntaxKind.Constructor && nodeIsPresent(func.body))) {
2891328913
error(node, Diagnostics.A_parameter_property_is_only_allowed_in_a_constructor_implementation);
2891428914
}
28915+
if (func.kind === SyntaxKind.Constructor && isIdentifier(node.name) && node.name.escapedText === "constructor") {
28916+
error(node.name, Diagnostics.constructor_cannot_be_used_as_a_parameter_property_name);
28917+
}
2891528918
}
2891628919
if (node.questionToken && isBindingPattern(node.name) && (func as FunctionLikeDeclaration).body) {
2891728920
error(node, Diagnostics.A_binding_pattern_parameter_cannot_be_optional_in_an_implementation_signature);

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,6 +1569,10 @@
15691569
"category": "Error",
15701570
"code": 2397
15711571
},
1572+
"'constructor' cannot be used as a parameter property name.": {
1573+
"category": "Error",
1574+
"code": 2398
1575+
},
15721576
"Duplicate identifier '_this'. Compiler uses variable declaration '_this' to capture 'this' reference.": {
15731577
"category": "Error",
15741578
"code": 2399
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/compiler/parameterPropertyInConstructor3.ts(2,22): error TS2398: 'constructor' cannot be used as a parameter property name.
2+
3+
4+
==== tests/cases/compiler/parameterPropertyInConstructor3.ts (1 errors) ====
5+
class Foo {
6+
constructor(public constructor: string) {}
7+
~~~~~~~~~~~
8+
!!! error TS2398: 'constructor' cannot be used as a parameter property name.
9+
}
10+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [parameterPropertyInConstructor3.ts]
2+
class Foo {
3+
constructor(public constructor: string) {}
4+
}
5+
6+
7+
//// [parameterPropertyInConstructor3.js]
8+
var Foo = /** @class */ (function () {
9+
function Foo(constructor) {
10+
this.constructor = constructor;
11+
}
12+
return Foo;
13+
}());
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/parameterPropertyInConstructor3.ts ===
2+
class Foo {
3+
>Foo : Symbol(Foo, Decl(parameterPropertyInConstructor3.ts, 0, 0))
4+
5+
constructor(public constructor: string) {}
6+
>constructor : Symbol(Foo.constructor, Decl(parameterPropertyInConstructor3.ts, 1, 14))
7+
}
8+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/parameterPropertyInConstructor3.ts ===
2+
class Foo {
3+
>Foo : Foo
4+
5+
constructor(public constructor: string) {}
6+
>constructor : string
7+
}
8+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Foo {
2+
constructor(public constructor: string) {}
3+
}

0 commit comments

Comments
 (0)