I have two interfaces that extend a third one, for example
interface Animal { /* name, etc */ };
interface Dog extends Animal { /* dog specific things */ }
interface Cat extends Animal { /* cat specific things */ }
I want to create for the two extending classes "Model" classes, that simplify the work with the database
interface AnimalModel<A extends Animal> {
all(): Promise<A[]>;
get(id: string): Promise<A>;
create(user: A): Promise<void>;
}
I went ahead and
class DogModel implements AnimalModel<Dog> {
// Expected TypeScript to complain, but it does not, because Cat does extend the animal?
async create(cat: Cat) { /* actual implementation, query the db */ }
}
What am I doing wrong? I expected class DogModel implements AnimalModel<Dog>
methods to only accept and/or return Dog
s, but it can be any Animal
.
ps: I'm thinking about using an abstract class, so I might solve it differently, the question is still unanswered and I want to understand what is wrong with my code (the one in this question)