Skip to content

Commit 57775ed

Browse files
author
Josh Goldberg
authored
Consider class field properties to redeclare parent definitions (microsoft#43194)
1 parent c34b252 commit 57775ed

9 files changed

+134
-1
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27119,7 +27119,7 @@ namespace ts {
2711927119
if (isInPropertyInitializer(node)
2712027120
&& !(isAccessExpression(node) && isAccessExpression(node.expression))
2712127121
&& !isBlockScopedNameDeclaredBeforeUse(valueDeclaration, right)
27122-
&& !isPropertyDeclaredInAncestorClass(prop)) {
27122+
&& (compilerOptions.useDefineForClassFields || !isPropertyDeclaredInAncestorClass(prop))) {
2712327123
diagnosticMessage = error(right, Diagnostics.Property_0_is_used_before_its_initialization, declarationName);
2712427124
}
2712527125
else if (valueDeclaration.kind === SyntaxKind.ClassDeclaration &&
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
tests/cases/conformance/classes/propertyMemberDeclarations/redeclaredProperty.ts(7,12): error TS2729: Property 'b' is used before its initialization.
2+
3+
4+
==== tests/cases/conformance/classes/propertyMemberDeclarations/redeclaredProperty.ts (1 errors) ====
5+
class Base {
6+
b = 1;
7+
}
8+
9+
class Derived extends Base {
10+
b;
11+
d = this.b;
12+
~
13+
!!! error TS2729: Property 'b' is used before its initialization.
14+
!!! related TS2728 tests/cases/conformance/classes/propertyMemberDeclarations/redeclaredProperty.ts:6:3: 'b' is declared here.
15+
16+
constructor() {
17+
super();
18+
this.b = 2;
19+
}
20+
}
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//// [redeclaredProperty.ts]
2+
class Base {
3+
b = 1;
4+
}
5+
6+
class Derived extends Base {
7+
b;
8+
d = this.b;
9+
10+
constructor() {
11+
super();
12+
this.b = 2;
13+
}
14+
}
15+
16+
17+
//// [redeclaredProperty.js]
18+
class Base {
19+
b = 1;
20+
}
21+
class Derived extends Base {
22+
b;
23+
d = this.b;
24+
constructor() {
25+
super();
26+
this.b = 2;
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
tests/cases/conformance/classes/propertyMemberDeclarations/redefinedPararameterProperty.ts(6,14): error TS2729: Property 'a' is used before its initialization.
2+
3+
4+
==== tests/cases/conformance/classes/propertyMemberDeclarations/redefinedPararameterProperty.ts (1 errors) ====
5+
class Base {
6+
a = 1;
7+
}
8+
9+
class Derived extends Base {
10+
b = this.a /*undefined*/;
11+
~
12+
!!! error TS2729: Property 'a' is used before its initialization.
13+
!!! related TS2728 tests/cases/conformance/classes/propertyMemberDeclarations/redefinedPararameterProperty.ts:8:17: 'a' is declared here.
14+
15+
constructor(public a: number) {
16+
super();
17+
}
18+
}
19+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//// [redefinedPararameterProperty.ts]
2+
class Base {
3+
a = 1;
4+
}
5+
6+
class Derived extends Base {
7+
b = this.a /*undefined*/;
8+
9+
constructor(public a: number) {
10+
super();
11+
}
12+
}
13+
14+
15+
//// [redefinedPararameterProperty.js]
16+
class Base {
17+
a = 1;
18+
}
19+
class Derived extends Base {
20+
a;
21+
b = this.a /*undefined*/;
22+
constructor(a) {
23+
super();
24+
this.a = a;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
=== tests/cases/conformance/classes/propertyMemberDeclarations/redefinedPararameterProperty.ts ===
2+
3+
No type information for this code.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
=== tests/cases/conformance/classes/propertyMemberDeclarations/redefinedPararameterProperty.ts ===
2+
3+
No type information for this code.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// @noTypesAndSymbols: true
2+
// @strictNullChecks: true
3+
// @target: esnext
4+
// @useDefineForClassFields: true
5+
class Base {
6+
b = 1;
7+
}
8+
9+
class Derived extends Base {
10+
b;
11+
d = this.b;
12+
13+
constructor() {
14+
super();
15+
this.b = 2;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @noTypesAndSymbols: true
2+
// @strictNullChecks: true
3+
// @target: esnext
4+
// @useDefineForClassFields: true
5+
class Base {
6+
a = 1;
7+
}
8+
9+
class Derived extends Base {
10+
b = this.a /*undefined*/;
11+
12+
constructor(public a: number) {
13+
super();
14+
}
15+
}
16+

0 commit comments

Comments
 (0)