Disclosure

Most of the problems under my TypeHero Challenges folder were either obtained from typehero.dev or from type-challenges repo. Purpose of these articles are just to document my approaches for my easy reference. Please visit the respective links for more info.

Link to original

Problem Description

Pick is a special built-in TypeScript type that’s always in scope, so you don’t need to import it from anywhere. You can just use it as-if it was already imported.

To use it, you must provide Pick with two things:

  1. The type original object you’re basing your new type on (in our example, Pokemon)
  2. union of strings representing all the keys you wish to pick from the base type (in our case, name and type).
interface Pokemon {
  name: string;
  type: string;
  hitPoints: number;
  stage: string;
}
 
type BasicPokemonInfo = Pick<Pokemon, 'name' | 'type'>;

In this challenge you’ll implement the Pick type described above.

Solutions

Approach

type MyPick<T, K extends keyof T> = {
	[Key in K]: T[Key]
};