缩小通用类型,编译器不高兴

Narrowing a Generic type, compiler is unhappy

鉴于这种有点折磨人的设置:

enum Mammal {
    Monkey= 'monkey',
    Mouse = 'mouse',
    Cat = 'cat'
}
enum Reptile {
    Lizard='lizard',
    Snake='snake'
}

type ReptileFood = {
    [Reptile.Lizard]: {foodType:'bugs'}
    [Reptile.Snake]: {foodType:'mice'}
}
type MammalFood = {
    [Mammal.Monkey]: {foodType:'bananas'} 
    [Mammal.Mouse]:  {foodType:'cheese'}
    [Mammal.Cat]:  {foodType:'fish'}
}
type ValueOf<T> = T[keyof T];

type Animal = keyof MammalFood | keyof ReptileFood
type AnimalFood = ReptileFood & MammalFood

function feedAnimal(animal:Animal, food:ValueOf<AnimalFood>):void{
    console.log(`The ${animal} has eaten the ${food.foodType}`)
}

...我想为哺乳动物创建一个特例 feedAnimal

function feedMammal<FoodType extends MammalFood>(animal:keyof MammalFood, food:ValueOf<FoodType>){
    feedAnimal(animal, food)
}

但是编译器对 feedAnimal 调用中 food 的类型不满意:

 Argument of type 'ValueOf<FoodType>' is not assignable to parameter of type 'ValueOf<AnimalFood>'.
  Type 'FoodType[string] | FoodType[number] | FoodType[symbol]' is not assignable to type 'ValueOf<AnimalFood>'.
    Type 'FoodType[string]' is not assignable to type 'ValueOf<AnimalFood>'.
      Type 'FoodType[string]' is not assignable to type '{ foodType: "fish"; }'.
        Type 'ValueOf<FoodType>' is not assignable to type '{ foodType: "fish"; }'.
          Type 'FoodType[string] | FoodType[number] | FoodType[symbol]' is not assignable to type '{ foodType: "fish"; }'.
            Type 'FoodType[string]' is not assignable to type '{ foodType: "fish"; }'.

如何让它明白一切都会好的?

Playground here

也许你想要

function feedMammal<M extends Mammal>(animal: M, food: MammalFood[M]) {
    feedAnimal(animal, food);
}