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.
This commit is contained in:
parent
f31b5a278f
commit
3ffe253166
5 changed files with 41 additions and 6 deletions
|
@ -33373,8 +33373,7 @@ namespace ts {
|
||||||
const derivedPropertyFlags = derived.flags & SymbolFlags.PropertyOrAccessor;
|
const derivedPropertyFlags = derived.flags & SymbolFlags.PropertyOrAccessor;
|
||||||
if (basePropertyFlags && derivedPropertyFlags) {
|
if (basePropertyFlags && derivedPropertyFlags) {
|
||||||
// property/accessor is overridden with property/accessor
|
// property/accessor is overridden with property/accessor
|
||||||
if (!compilerOptions.useDefineForClassFields
|
if (baseDeclarationFlags & ModifierFlags.Abstract && !(base.valueDeclaration && isPropertyDeclaration(base.valueDeclaration) && base.valueDeclaration.initializer)
|
||||||
|| baseDeclarationFlags & ModifierFlags.Abstract && !(base.valueDeclaration && isPropertyDeclaration(base.valueDeclaration) && base.valueDeclaration.initializer)
|
|
||||||
|| base.valueDeclaration && base.valueDeclaration.parent.kind === SyntaxKind.InterfaceDeclaration
|
|| base.valueDeclaration && base.valueDeclaration.parent.kind === SyntaxKind.InterfaceDeclaration
|
||||||
|| derived.valueDeclaration && isBinaryExpression(derived.valueDeclaration)) {
|
|| derived.valueDeclaration && isBinaryExpression(derived.valueDeclaration)) {
|
||||||
// when the base property is abstract or from an interface, base/derived flags don't need to match
|
// when the base property is abstract or from an interface, base/derived flags don't need to match
|
||||||
|
@ -33390,7 +33389,7 @@ namespace ts {
|
||||||
Diagnostics._0_is_defined_as_a_property_in_class_1_but_is_overridden_here_in_2_as_an_accessor;
|
Diagnostics._0_is_defined_as_a_property_in_class_1_but_is_overridden_here_in_2_as_an_accessor;
|
||||||
error(getNameOfDeclaration(derived.valueDeclaration) || derived.valueDeclaration, errorMessage, symbolToString(base), typeToString(baseType), typeToString(type));
|
error(getNameOfDeclaration(derived.valueDeclaration) || derived.valueDeclaration, errorMessage, symbolToString(base), typeToString(baseType), typeToString(type));
|
||||||
}
|
}
|
||||||
else {
|
else if (compilerOptions.useDefineForClassFields) {
|
||||||
const uninitialized = find(derived.declarations, d => d.kind === SyntaxKind.PropertyDeclaration && !(d as PropertyDeclaration).initializer);
|
const uninitialized = find(derived.declarations, d => d.kind === SyntaxKind.PropertyDeclaration && !(d as PropertyDeclaration).initializer);
|
||||||
if (uninitialized
|
if (uninitialized
|
||||||
&& !(derived.flags & SymbolFlags.Transient)
|
&& !(derived.flags & SymbolFlags.Transient)
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
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.
|
||||||
|
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.
|
||||||
|
|
||||||
|
|
||||||
|
==== tests/cases/conformance/classes/propertyMemberDeclarations/accessorsOverrideProperty6.ts (2 errors) ====
|
||||||
|
class A {
|
||||||
|
p = 'yep'
|
||||||
|
}
|
||||||
|
class B extends A {
|
||||||
|
get p() { return 'oh no' } // error
|
||||||
|
~
|
||||||
|
!!! error TS2611: 'p' is defined as a property in class 'A', but is overridden here in 'B' as an accessor.
|
||||||
|
}
|
||||||
|
class C {
|
||||||
|
p = 101
|
||||||
|
}
|
||||||
|
class D extends C {
|
||||||
|
_secret = 11
|
||||||
|
get p() { return this._secret } // error
|
||||||
|
~
|
||||||
|
!!! error TS2611: 'p' is defined as a property in class 'C', but is overridden here in 'D' as an accessor.
|
||||||
|
set p(value) { this._secret = value } // error
|
||||||
|
}
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
tests/cases/compiler/inheritanceMemberAccessorOverridingProperty.ts(6,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
|
tests/cases/compiler/inheritanceMemberAccessorOverridingProperty.ts(6,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
|
||||||
|
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.
|
||||||
tests/cases/compiler/inheritanceMemberAccessorOverridingProperty.ts(9,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
|
tests/cases/compiler/inheritanceMemberAccessorOverridingProperty.ts(9,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
|
||||||
|
|
||||||
|
|
||||||
==== tests/cases/compiler/inheritanceMemberAccessorOverridingProperty.ts (2 errors) ====
|
==== tests/cases/compiler/inheritanceMemberAccessorOverridingProperty.ts (3 errors) ====
|
||||||
class a {
|
class a {
|
||||||
x: string;
|
x: string;
|
||||||
}
|
}
|
||||||
|
@ -11,6 +12,8 @@ tests/cases/compiler/inheritanceMemberAccessorOverridingProperty.ts(9,9): error
|
||||||
get x() {
|
get x() {
|
||||||
~
|
~
|
||||||
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
|
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
|
||||||
|
~
|
||||||
|
!!! error TS2611: 'x' is defined as a property in class 'a', but is overridden here in 'b' as an accessor.
|
||||||
return "20";
|
return "20";
|
||||||
}
|
}
|
||||||
set x(aValue: string) {
|
set x(aValue: string) {
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
tests/cases/compiler/inheritanceMemberPropertyOverridingAccessor.ts(3,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
|
tests/cases/compiler/inheritanceMemberPropertyOverridingAccessor.ts(3,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
|
||||||
tests/cases/compiler/inheritanceMemberPropertyOverridingAccessor.ts(6,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
|
tests/cases/compiler/inheritanceMemberPropertyOverridingAccessor.ts(6,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
|
||||||
|
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.
|
||||||
|
|
||||||
|
|
||||||
==== tests/cases/compiler/inheritanceMemberPropertyOverridingAccessor.ts (2 errors) ====
|
==== tests/cases/compiler/inheritanceMemberPropertyOverridingAccessor.ts (3 errors) ====
|
||||||
class a {
|
class a {
|
||||||
private __x: () => string;
|
private __x: () => string;
|
||||||
get x() {
|
get x() {
|
||||||
|
@ -19,4 +20,6 @@ tests/cases/compiler/inheritanceMemberPropertyOverridingAccessor.ts(6,9): error
|
||||||
|
|
||||||
class b extends a {
|
class b extends a {
|
||||||
x: () => string;
|
x: () => string;
|
||||||
|
~
|
||||||
|
!!! error TS2610: 'x' is defined as an accessor in class 'a', but is overridden here in 'b' as an instance property.
|
||||||
}
|
}
|
|
@ -37,7 +37,13 @@ Output::
|
||||||
[[90m12:00:13 AM[0m] Starting compilation in watch mode...
|
[[90m12:00:13 AM[0m] Starting compilation in watch mode...
|
||||||
|
|
||||||
|
|
||||||
[[90m12:00:16 AM[0m] Found 0 errors. Watching for file changes.
|
[96ma.ts[0m:[93m2[0m:[93m21[0m - [91merror[0m[90m TS2610: [0m'prop' is defined as an accessor in class 'C', but is overridden here in 'D' as an instance property.
|
||||||
|
|
||||||
|
[7m2[0m class D extends C { prop = 1; }
|
||||||
|
[7m [0m [91m ~~~~[0m
|
||||||
|
|
||||||
|
|
||||||
|
[[90m12:00:16 AM[0m] Found 1 error. Watching for file changes.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue