TypeScript Pick и Record

Определение типов:

type Pick<T, K extends keyof T> = {
    [P in K]: T[P];
}
type Record<K extends string, T> = {
    [P in K]: T;
}

Pick

interface Dog {
  id?: number;
  name: string;
  weight: number;
  age: number;
}

type Doggy = Pick<Dog, 'id' | 'name' >;

// эквивалентно
type Doggy = {
  id?: number;
  name: string;
}

Record

type ThreeDogProps = Record<'prop1' | 'prop2' | 'prop3', Dog>;

// эквивалентно
type ThreeDogProps = {
  prop1: Dog;
  prop2: Dog;
  prop3: Dog;
}

Похожие записи