class Animal<T extends string> {
eat(food: T): void {
console.log('eating ' + food)
}
}
class Rabbit extends Animal<'carrot'> {}
class Cat extends Animal<'mouse'>{}
type MyPet = Rabbit | Cat
const myPets: Record<string, MyPet> = {
'aa': new Rabbit(),
'bb': new Cat()
}
function getPets (name: string): MyPet {
return myPets[name]
}
const pet = getPets('aa')
// error: Argument of type 'string' is not assignable to parameter of type 'never'.
pet.eat('carrot')
pet.eat('mouse')
Please check the source code [source code replication], The generic method is called by using the joint type of two subclasses. The parameter type is inferred as never. Shouldn't it be carrot | mouseć