Loading
Current section: 5. Utility Types 6 exercises
solution

Partial Pick

Transcript

00:00 Alright. You're all done? So now we're gonna take this user, and we're going to get a partial user. So what this is going to do is it's gonna take each one of these properties, which are required by default, and it's going to say this any one of these values are is optional. And you might see something kinda interesting.

00:19 So, this is how you make a a value optional, but it also adds or undefined on each one of these. And that what that effectively is saying is you can have it be specified as long or and it's okay that it's undefined. So it can be specified as undefined. It doesn't have to either be a string or not exist. It can also exist but be set to undefined.

00:42 That's all that, the reason that that is there. Okay. So then let's use, user username email that includes only the name and email. We're gonna use pick on the user and we're gonna pick the properties name or email. So we're gonna make a new type that has name or email from the user and that's gonna give us the username email.

01:05 And now, we're going to have a type user update. And this is going to allow us to optionally update only the name and email. So we're gonna combine these together. First, we're going to pick the, pick apart the user, for just the name and email, and then we're gonna make that partial, resulting in a user update which allows us to update the name or the email. And if name is not included, it'll just update the email.

01:30 If the email is not included, it'll just update the use the name. And that is going to be encapsulated in this update user where we take, the given user object and we take the updates. And what's cool about this is it allows us to update the name or the email, and update both, but it's not going to allow us to update the ID. And that is one of the nice things about partial and pick, is well, first of all, pick is useful because it's not gonna allow us to update the ID. Partial is useful because it allows us to update just one of them or both of them if we like.

02:07 And, yeah, you can go ahead and, uncomment these console logs. And let's comment that out. Actually, here. Let's go ahead and console log the updated four. And here we're going to see, right here we actually did change the ID and that is not desirable.

02:24 This is why TypeScript is so nice. So JavaScript will let you do it, but TypeScript will stop you and that's why we love job or TypeScript. So there you go. That is partial and pick.