Learn TypeScript’s “Pick” in 1 Minute: Choose Only What You Need
When you work with TypeScript, sometimes you don’t need all the properties from an object or type. You just want a few specific ones. This is where Pick comes in handy.
What is Pick?
Pick is a built-in TypeScript utility that lets you create a new type by selecting only some properties from an existing type.
How Does It Work?
Imagine you have a big object type called Post:
But for some function, you only want the title and content. Instead of copying the whole type, you use Pick:
Now, PostPreview only has title and content.
Why Use Pick?
Example: Using Pick in a Function
This function only expects title and content, ignoring other fields.
Summary
Pick helps you create smaller, focused types from bigger ones. It’s perfect for when you want to work with just a part of an object without repeating type definitions.
Try it out and make your TypeScript code cleaner and safer!