Identity Functions 5 exercises

Add Constraints to an Identity Function

Let's continue with the fruits example.

Here we have a narrowFruits function that looks a lot like the the asConst function from the previous exercise:

export const narrowFruits = <TFruits>(t: TFruits) => t;

However this time we want to narrow the type to only work with a

Loading exercise


0:00 In this exercise, we have a similar kind of function that we had before, where we want to capture the name and price of these fruits, but we also want to make sure that nothing else can be passed in here.

0:13 You shouldn't be able to pass in an array of strings, shouldn't be able to pass in strings, shouldn't be able to pass in anything except something that's constrained to name, "apple," price 1, and an array of those, of course.

0:24 We still want to capture the same type of data as in the previous exercise, but we want it to be constrained. There's an interesting little challenge here for you, and there's a couple of little gotchas here.

0:35 I'm not going to tell you what those are. I think you might know the basics. You know how to constrain a type annotation, and you know how to make something const. I'll leave you to figure out the rest. Good luck.