10-1. Mapped Types 1 - Partial, Required, Readonly

Mapped Types에 대한 정의는 아래를 참고

8-4. 맵드 타입 Mapped Types

우선 아래와 같은 블로그 게시글 객체 타입을 정의하고 시작하자.

type Post = {
  title: string;
  tags: string[];
  content: string;
  thumbnailURL?: string;
}

Partial

/* Partial<T> */
type Partial<T> = {
  [key in keyof T]?: T[key];
};

const draft: Partial<Post> = {
  title: "title",
  content: "draft",
}

Required

/* Required<T> */

type Required<T> = {
  [key in keyof T]-?: T[key];
}

const withTumbnailPost: Required<Post> = {
  title: "one bite TS review",
  tags: ["TS", "Front-end"],
  content: "content",
  thumbnailURL: "https://...",
}