Skip to content

Consider class field properties to redeclare parent definitions #43194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26861,7 +26861,7 @@ namespace ts {
if (isInPropertyInitializer(node)
&& !(isAccessExpression(node) && isAccessExpression(node.expression))
&& !isBlockScopedNameDeclaredBeforeUse(valueDeclaration, right)
&& !isPropertyDeclaredInAncestorClass(prop)) {
&& (compilerOptions.useDefineForClassFields || !isPropertyDeclaredInAncestorClass(prop))) {
diagnosticMessage = error(right, Diagnostics.Property_0_is_used_before_its_initialization, declarationName);
}
else if (valueDeclaration.kind === SyntaxKind.ClassDeclaration &&
Expand Down
21 changes: 21 additions & 0 deletions tests/baselines/reference/redeclaredProperty.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
tests/cases/conformance/classes/propertyMemberDeclarations/redeclaredProperty.ts(7,12): error TS2729: Property 'b' is used before its initialization.


==== tests/cases/conformance/classes/propertyMemberDeclarations/redeclaredProperty.ts (1 errors) ====
class Base {
b = 1;
}

class Derived extends Base {
b;
d = this.b;
~
!!! error TS2729: Property 'b' is used before its initialization.
!!! related TS2728 tests/cases/conformance/classes/propertyMemberDeclarations/redeclaredProperty.ts:6:3: 'b' is declared here.

constructor() {
super();
this.b = 2;
}
}

28 changes: 28 additions & 0 deletions tests/baselines/reference/redeclaredProperty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [redeclaredProperty.ts]
class Base {
b = 1;
}

class Derived extends Base {
b;
d = this.b;

constructor() {
super();
this.b = 2;
}
}


//// [redeclaredProperty.js]
class Base {
b = 1;
}
class Derived extends Base {
b;
d = this.b;
constructor() {
super();
this.b = 2;
}
}
19 changes: 19 additions & 0 deletions tests/baselines/reference/redefinedPararameterProperty.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
tests/cases/conformance/classes/propertyMemberDeclarations/redefinedPararameterProperty.ts(6,14): error TS2729: Property 'a' is used before its initialization.


==== tests/cases/conformance/classes/propertyMemberDeclarations/redefinedPararameterProperty.ts (1 errors) ====
class Base {
a = 1;
}

class Derived extends Base {
b = this.a /*undefined*/;
~
!!! error TS2729: Property 'a' is used before its initialization.
!!! related TS2728 tests/cases/conformance/classes/propertyMemberDeclarations/redefinedPararameterProperty.ts:8:17: 'a' is declared here.

constructor(public a: number) {
super();
}
}

26 changes: 26 additions & 0 deletions tests/baselines/reference/redefinedPararameterProperty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//// [redefinedPararameterProperty.ts]
class Base {
a = 1;
}

class Derived extends Base {
b = this.a /*undefined*/;

constructor(public a: number) {
super();
}
}


//// [redefinedPararameterProperty.js]
class Base {
a = 1;
}
class Derived extends Base {
a;
b = this.a /*undefined*/;
constructor(a) {
super();
this.a = a;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
=== tests/cases/conformance/classes/propertyMemberDeclarations/redefinedPararameterProperty.ts ===

No type information for this code.
3 changes: 3 additions & 0 deletions tests/baselines/reference/redefinedPararameterProperty.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
=== tests/cases/conformance/classes/propertyMemberDeclarations/redefinedPararameterProperty.ts ===

No type information for this code.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// @noTypesAndSymbols: true
// @strictNullChecks: true
// @target: esnext
// @useDefineForClassFields: true
class Base {
b = 1;
}

class Derived extends Base {
b;
d = this.b;

constructor() {
super();
this.b = 2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @noTypesAndSymbols: true
// @strictNullChecks: true
// @target: esnext
// @useDefineForClassFields: true
class Base {
a = 1;
}

class Derived extends Base {
b = this.a /*undefined*/;

constructor(public a: number) {
super();
}
}