TS2611: 'foo' is defined as a property in class 'A', but is overridden here in 'B' as an accessor
The error message is correct as there is a subtle bug here. When you define a property in a parent class, the property is created automatically for every one of its sub-classes.
So, in the code you wrote, had you not written for class B: set/get foo(name) { ... }
the class would have had the foo
property anyway since the property is declared in B
's parent class - A
.
In your code you're actually clobbering the foo
property declared in A
with the accessor declarations.
If you want to require all sub-classes of A
to declare their own foo
property (similar to an interface), then try:
export class A {
abstract foo: string;
}
It seems it's enforced in ts 4: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-0.html#properties-overriding-accessors-and-vice-versa-is-an-error