I want classes that implement the Observer interface to also implement the Comparable interface, how can I do this?
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'.
But, TestA implements the Observer interface why are they not compatible?
Of course, I can write like this:
class TestA implements Observer {
private name = '';
equals = (item: Observer) => {
return this.name === item.name
}
notify = () => {}
}
But then I get such an error, and besides, this is not entirely correct, because I want to compare only objects of this class:
Property 'name' does not exist on type 'Observer'.
How to do it right? "typescript": "^3.9.2"