Challenges 7 exercises
Problem

Create a Pick Function

Let's build a classic pick function.

The function takes in an object and an array of keys, then returns the keys that you expect at both the runtime level and the type level:


const pick = (obj: {}, picked: string[]) => {
return picked.reduce((acc, key) => {
acc[key] = obj[key

Loading exercise

Transcript

0:00 In this exercise, we're building a classic pick function. This pick function basically takes in an object and then takes in an array of keys from that object. What it's going to do is you're supposed to take this pick function, apply it to an object, say the keys you want, and you should get back the keys that you expect.

0:20 Not only on the runtime level, which should be working right now but also on the type level. On the type level, you should be getting an A number and B number, but instead, we're just getting an empty object back.

0:33 There's also another test down here to make sure that you don't pass in something that isn't on the object. D shouldn't be able to be passed here because it's not a member of this object here. That's your challenge. I think you have everything you need. Remember everything that we have about the reduce function and how annoying it is.

0:55 You're definitely going to need at least one generic here. Remember about, I don't know, how to capture certain things in generic arguments and how many generic arguments you need. I think that's it. Good luck.