TypeScript 7 reference · Utility Types
TypeScript Pick<Type, Keys> Utility Type
Build a type from just some properties of another type. Handy for API responses and props that need a subset.
TypeScript 7 Since 2.1
Syntax
Pick<Type, Keys>
Parameters
| Parameter | Description |
|---|---|
Type |
The object type to transform. |
Keys |
A union of property names, e.g. "id" | "name". |
Return value
A type with only the picked properties.
Examples
interface User {
id: number;
name: string;
email: string;
passwordHash: string;
}
type PublicUser = Pick<User, "id" | "name">;
const u: PublicUser = { id: 1, name: "Ana" };
console.log(u);
Output
{ id: 1, name: 'Ana' }interface Todo {
title: string;
done: boolean;
due: string;
}
function preview(t: Pick<Todo, "title" | "done">) {
return `${t.done ? "[x]" : "[ ]"} ${t.title}`;
}
console.log(preview({ title: "Buy milk", done: false }));
Output
[ ] Buy milk
Version history
| Version | Change |
|---|---|
| TypeScript 2.1 | Added |
Related
Omit<Type, Keys>· Build a type with all properties of another type except the ones you list. The opposite of Pick.Record<Keys, Type>· Build an object type whose keys come from Keys and whose values all have Type. Great for lookup tables.- TypeScript Utility Types · tutorial