JS Built-ins
TypeScriptintermediate

Utility Types β€” Pick<T, K>

πŸ“– Usage Example
type Preview = Pick<{ id: number; name: string; bio: string }, 'id' | 'name'>
// Preview = { id: number; name: string }

Pick<T, K> constructs a new type with only the specified subset of properties from T.

Utility Types: Pick<T, K>

Pick<T, K> constructs a type with only the properties K from T.

Challenge: Implement pickKeys<T extends object, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>.

pickKeys({ a: 1, b: 2, c: 3 }, ['a', 'c'])  // β†’ { a: 1, c: 3 }
πŸ’‘ Hints (1)
  • β€’ Loop through keys and copy each to a new object.