有时,您可能希望根据某个属性在联合类型中查找类型

在此挑战中,我们想通过在联合类型 Cat | Dog 中通过指定公共属性 type 的值来获取相应的类型。换句话说,在以下示例中,LookUp<Dog | Cat, 'dog'> 的结果应该是 DogLookUp<Dog | Cat, 'cat'> 的结果应该是 Cat

interface Cat {
  type: 'cat'
  breeds: 'Abyssinian' | 'Shorthair' | 'Curl' | 'Bengal'
}
 
interface Dog {
  type: 'dog'
  breeds: 'Hound' | 'Brittany' | 'Bulldog' | 'Boxer'
  color: 'brown' | 'white' | 'black'
}
 
type MyDog = LookUp<Cat | Dog, 'dog'> // expected to be `Dog`