typescript Record

Tony·2021년 12월 9일
0

typescript

목록 보기
4/22

type을 서로 엮어서 만들 수 있다.

Record<Keys, Type>
Keys가 Record로 반환되는 타입의 키가 되고
Type은 각 Keys의 타입

/**
 * Construct a type with a set of properties K of type T
 */
type Record<K extends keyof any, T> = {
    [P in K]: T;
};
// e.g.,
type CatInfo = {
  age: number;
  breed: string;
}

type CatName = "miffy" | "boris" | "mordred";

const cats: Record<CatName, CatInfo> = {
  miffy: { age: 10, breed: "Persian" },
  boris: { age: 5, breed: "Maine Coon" },
  mordred: { age: 16, breed: "British Shorthair" },
};

console.log(cats.boris.age); // 5
profile
움직이는 만큼 행복해진다

0개의 댓글