I have a simple interface:
interface Animal<T> {
isMatch: (value: T) => boolean
}
with these implementations:
class Dog implements Animal<string> {
isMatch(input: string): boolean {
// do something with string
}
}
and
class Cat<T extends T[]> implements Animal<T> {
isMatch(input: T[]): boolean {
// do something with list
}
}
I can instantiate a Dog without issue with:
const dog = new Dog("pug")
However, I can't instantiate a Cat with any of:
const siamese = new Cat<string>(...) // Type 'string' does not satisfy the constraint 'string[]'
const persian = new Cat<string[]>(...) // Type 'string[]' does not satisfy the constraint 'string[][]'
const sphynx = new Cat(["birman"]) // Type 'string' does not satisfy the constraint 'string[]'
I've used the generics / extends based on examples I've seen online, so not sure what I'm misunderstanding here.