Skip to content

Commit 3ffe253

Browse files
committed
Always error when property overrides accessor or vice versa
Previously this was only an error when useDefineForClassFields: true, but now it's an error for all code. This is a rare mistake to make, and usually only occurs in code written before `readonly` was available. Codefix to come in subsequent commits.
1 parent f31b5a2 commit 3ffe253

File tree

5 files changed

+41
-6
lines changed

5 files changed

+41
-6
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33373,8 +33373,7 @@ namespace ts {
3337333373
const derivedPropertyFlags = derived.flags & SymbolFlags.PropertyOrAccessor;
3337433374
if (basePropertyFlags && derivedPropertyFlags) {
3337533375
// property/accessor is overridden with property/accessor
33376-
if (!compilerOptions.useDefineForClassFields
33377-
|| baseDeclarationFlags & ModifierFlags.Abstract && !(base.valueDeclaration && isPropertyDeclaration(base.valueDeclaration) && base.valueDeclaration.initializer)
33376+
if (baseDeclarationFlags & ModifierFlags.Abstract && !(base.valueDeclaration && isPropertyDeclaration(base.valueDeclaration) && base.valueDeclaration.initializer)
3337833377
|| base.valueDeclaration && base.valueDeclaration.parent.kind === SyntaxKind.InterfaceDeclaration
3337933378
|| derived.valueDeclaration && isBinaryExpression(derived.valueDeclaration)) {
3338033379
// when the base property is abstract or from an interface, base/derived flags don't need to match
@@ -33390,7 +33389,7 @@ namespace ts {
3339033389
Diagnostics._0_is_defined_as_a_property_in_class_1_but_is_overridden_here_in_2_as_an_accessor;
3339133390
error(getNameOfDeclaration(derived.valueDeclaration) || derived.valueDeclaration, errorMessage, symbolToString(base), typeToString(baseType), typeToString(type));
3339233391
}
33393-
else {
33392+
else if (compilerOptions.useDefineForClassFields) {
3339433393
const uninitialized = find(derived.declarations, d => d.kind === SyntaxKind.PropertyDeclaration && !(d as PropertyDeclaration).initializer);
3339533394
if (uninitialized
3339633395
&& !(derived.flags & SymbolFlags.Transient)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
tests/cases/conformance/classes/propertyMemberDeclarations/accessorsOverrideProperty6.ts(5,9): error TS2611: 'p' is defined as a property in class 'A', but is overridden here in 'B' as an accessor.
2+
tests/cases/conformance/classes/propertyMemberDeclarations/accessorsOverrideProperty6.ts(12,9): error TS2611: 'p' is defined as a property in class 'C', but is overridden here in 'D' as an accessor.
3+
4+
5+
==== tests/cases/conformance/classes/propertyMemberDeclarations/accessorsOverrideProperty6.ts (2 errors) ====
6+
class A {
7+
p = 'yep'
8+
}
9+
class B extends A {
10+
get p() { return 'oh no' } // error
11+
~
12+
!!! error TS2611: 'p' is defined as a property in class 'A', but is overridden here in 'B' as an accessor.
13+
}
14+
class C {
15+
p = 101
16+
}
17+
class D extends C {
18+
_secret = 11
19+
get p() { return this._secret } // error
20+
~
21+
!!! error TS2611: 'p' is defined as a property in class 'C', but is overridden here in 'D' as an accessor.
22+
set p(value) { this._secret = value } // error
23+
}
24+

tests/baselines/reference/inheritanceMemberAccessorOverridingProperty.errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
tests/cases/compiler/inheritanceMemberAccessorOverridingProperty.ts(6,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
2+
tests/cases/compiler/inheritanceMemberAccessorOverridingProperty.ts(6,9): error TS2611: 'x' is defined as a property in class 'a', but is overridden here in 'b' as an accessor.
23
tests/cases/compiler/inheritanceMemberAccessorOverridingProperty.ts(9,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
34

45

5-
==== tests/cases/compiler/inheritanceMemberAccessorOverridingProperty.ts (2 errors) ====
6+
==== tests/cases/compiler/inheritanceMemberAccessorOverridingProperty.ts (3 errors) ====
67
class a {
78
x: string;
89
}
@@ -11,6 +12,8 @@ tests/cases/compiler/inheritanceMemberAccessorOverridingProperty.ts(9,9): error
1112
get x() {
1213
~
1314
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
15+
~
16+
!!! error TS2611: 'x' is defined as a property in class 'a', but is overridden here in 'b' as an accessor.
1417
return "20";
1518
}
1619
set x(aValue: string) {

tests/baselines/reference/inheritanceMemberPropertyOverridingAccessor.errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
tests/cases/compiler/inheritanceMemberPropertyOverridingAccessor.ts(3,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
22
tests/cases/compiler/inheritanceMemberPropertyOverridingAccessor.ts(6,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
3+
tests/cases/compiler/inheritanceMemberPropertyOverridingAccessor.ts(12,5): error TS2610: 'x' is defined as an accessor in class 'a', but is overridden here in 'b' as an instance property.
34

45

5-
==== tests/cases/compiler/inheritanceMemberPropertyOverridingAccessor.ts (2 errors) ====
6+
==== tests/cases/compiler/inheritanceMemberPropertyOverridingAccessor.ts (3 errors) ====
67
class a {
78
private __x: () => string;
89
get x() {
@@ -19,4 +20,6 @@ tests/cases/compiler/inheritanceMemberPropertyOverridingAccessor.ts(6,9): error
1920

2021
class b extends a {
2122
x: () => string;
23+
~
24+
!!! error TS2610: 'x' is defined as an accessor in class 'a', but is overridden here in 'b' as an instance property.
2225
}

tests/baselines/reference/tscWatch/programUpdates/updates-diagnostics-and-emit-when-useDefineForClassFields-changes.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ Output::
3737
[12:00:13 AM] Starting compilation in watch mode...
3838

3939

40-
[12:00:16 AM] Found 0 errors. Watching for file changes.
40+
a.ts:2:21 - error TS2610: 'prop' is defined as an accessor in class 'C', but is overridden here in 'D' as an instance property.
41+
42+
2 class D extends C { prop = 1; }
43+
   ~~~~
44+
45+
46+
[12:00:16 AM] Found 1 error. Watching for file changes.
4147

4248

4349

0 commit comments

Comments
 (0)