Type Lookup medium #union #map

by Anthony Fu @antfu

Take the Challenge    简体中文 日本語 한국어

Sometimes, you may want to look up a type in a union by its 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` and `Cat` for `LookUp` in the following example. ```ts 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 // expected to be `Dog` ```
Back Share your Solutions Check out Solutions