Conditional Types and Infer 10 exercises

Introducing infer for Conditional Logic

In this exercise we're going to take our first look at infer.

Before you go any further, please take some time to carefully read over this section of the TypeScript docs.

It's important that you start building an understanding of what's going on with infer before moving on to this challenge.


Loading exercise


0:01 In this section, we're going to learn about something called infer. Now, infer can confuse a lot of people, so make sure you read the docs really, really carefully and try to understand what's going on with it before you process even what this task is doing.

0:16 Here, we're creating a function called GetDataValue -- sorry, a type function -- where we should be able to pass in something that has a data attribute here. This data attribute, we basically want to return from this type function whatever is in that data slot.

0:34 Here, this hello is being extracted out there. Here, we have name hello. That's being extracted out there too. In here, we have data name hello age 20, blah, blah, blah, blah, blah. If it doesn't conform to that, we should be returning never as well.

0:52 That's your job. We probably need to do some kind of conditional check to make sure that the T here is being passed in as data. If it is being passed in as data, we need to then extract out that information in order to return it.