Sometimes, you may want to look up a type in a union by its attributes. Learn union type manipulation in this medium-level challenge on TypeScriptPro.
Sometimes, you may want to look up a type in a union by its attributes. 🔀
In this medium-level challenge, you'll sometimes, you may want to look up a type in a union by its attributes.. Learn union type manipulation in this medium-level challenge on TypeScriptPro.
You'll learn about union type manipulation, essential skills for advanced TypeScript development and type-level programming.
For this challenge, you will need to change the following code to make the tests pass (no type check errors).
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<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`
This challenge requires understanding of union type manipulation.
The solution involves carefully constructing types that satisfy all test cases while handling edge cases properly.
This challenge helps you understand union type manipulation and how to apply this concept in real-world scenarios.
Be the first to access the course, unlock exclusive launch bonuses, and get special early-bird pricing before anyone else.
Only 27 Spots left
Get 1 month early access
Pre-Launch discount
This challenge is originally from here.