Learn TypeScript’s “Pick” in 1 Minute: Choose Only What You Need

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:

Article content

But for some function, you only want the title and content. Instead of copying the whole type, you use Pick:

Article content

Now, PostPreview only has title and content.

Why Use Pick?

  • Cleaner code: You only work with what you need.
  • Better safety: Avoid using or changing unnecessary data.
  • Easy validation: Validate only the fields you want.

Example: Using Pick in a Function

Article content

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!

To view or add a comment, sign in

Others also viewed

Explore topics