Type Lookup
Challenge
Sometimes, you may want to lookup for a type in a union to by their attributes.
In this challenge, we would like to get the corresponding type by searching for the common type field in the union Cat | Dog. In other words, we will expect to get Dog for LookUp<Dog | Cat, 'dog'> and Cat for LookUp<Dog | Cat, 'cat'> in the following example.
interface Cat {
type: "cat";
breeds: "Abyssinian" | "Shorthair" | "Curl" | "Bengal";
}
interface Dog {
type: "dog";
breeds: "Hound" | "Brittany" | "Bulldog" | "Boxer";
color: "brown" | "white" | "black";
}
type MyDogType = LookUp<Cat | Dog, "dog">; // expected to be `Dog`
Solution
Die Lösung ist es zu prüfen, ob T ein Objekttyp erweitert, der die Eigenschaft type mit dem Wert von U beinhaltet. Hierfür können wir
einfach einen Konditionellen Typen nutzen.
type LookUp<T, U extends string> = T extends { type: U } ? T : never;