Type Transformations Workshop (9 exercises)

Introducing infer for Conditional Logic

Here we have a type function called GetDataValue:

The goal is to be able to pass an object with a data attribute into GetDataValue and have it return whatever value is there, as seen in our tests:

If the data attribute isn't there, the function should return never.

Hint: You probably need to do some conditional check to see if the T is being passed in as data. If it is, extract the information out of it in order to return it.


[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.