Consider class field properties to redeclare parent definitions (#43194)

This commit is contained in:
Josh Goldberg 2021-03-29 20:20:25 -04:00 committed by GitHub
parent c34b252e1e
commit 57775ed405
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 134 additions and 1 deletions

View File

@ -27119,7 +27119,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 &&

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,3 @@
=== tests/cases/conformance/classes/propertyMemberDeclarations/redefinedPararameterProperty.ts ===
No type information for this code.

View File

@ -0,0 +1,3 @@
=== tests/cases/conformance/classes/propertyMemberDeclarations/redefinedPararameterProperty.ts ===
No type information for this code.

View File

@ -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;
}
}

View File

@ -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();
}
}