Create a Type with Some Fields of Another Type in TypeScript with Pick

Kamran Ahmed
InstructorKamran Ahmed
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

There might be cases where you have selective data for your entities. Let's say that you are building a public API endpoint to get all the registered users from your users collection. Now there might be sensitive data in your User entity type that you may not want to return in the response. In such cases, Pick can help you be selective and get only the properties you need.

In this lesson, we will learn how to extract properties from a type and create a new type from it.

Instructor: [00:00] Here we have an item. Let's say that we want a preview for it where we will have only the name and the image of the item.

[00:06] To do that, we'll create a new type called itemPeview, and pick from our item type only the name and the image. We can create a preview for our item with the help of this type. I'll create a variable called itemPreview, which will use itemPreview as our type.

[00:25] We'll put the name here, item.name, and image is item.image. If I log it, itemPreview. We log that and there are no errors. If you try to put a description here, we can't put that, because it is not a part of our itemPreview type, because it is only picking up name and image from here and discarding all the other fields.

[00:52] If I remove it, there it will go away.

vamsg007
vamsg007
~ 7 months ago

Hi! Nice videos. is the closed captions feature broken or not available on this series?

Markdown supported.
Become a member to join the discussionEnroll Today