Is there a way to have classes that implement both the Observer and Comparable interfaces together?
interface Comparable<T> {
equals: (item: T) => boolean;
}
interface Observer extends Comparable<Observer> {
notify: () => void
}
class TestA implements Observer {
private name = '';
equals = (item: TestA) => {
return this.name === item.name
}
notify = () => {}
}
class TestB implements Observer {
private name = '';
equals = (item: TestB) => {
return this.name === item.name
}
notify = () => {}
}
Error:
TS2416: Property 'equals' in type 'TestA' is not assignable to the same property in base type 'Observer'. Type '(item: TestA) => boolean' is not assignable to type '(item: Observer) => boolean'. Types of parameters 'item' and 'item' are incompatible. Property 'name' is missing in type 'Observer' but required in type 'TestA'.
Even though TestA implements the Observer interface, they are not compatible. How can we resolve this issue?
We could modify like this:
class TestA implements Observer {
private name = '';
equals = (item: Observer) => {
return this.name === item.name
}
notify = () => {}
}
However, this leads to an error as well since it doesn't account for comparing objects of different classes:
Property 'name' does not exist on type 'Observer'.
How do we go about implementing this correctly? "typescript": "^3.9.2"