TypeScript - Utility Types 2

CH_Hwang·2022년 5월 30일
0

TypeScript

목록 보기
11/12

Pick<Type, Keys>

Pick<Type, Keys>은 타입에서 해당 키 property만 빼오는 것이다.

interface Todo {
  title: string;
  description: string;
  completed: boolean;
}

type TodoPreview = Pick<Todo, "title">

const todo: TodoPreview = {
  title: "Clean roon"
}

Omit<Type, Keys>

Omit<Type, Keys>은 Pick과 정 반대로 타입에서 해당 키 프로퍼티를 제외한 모든것을 뜻한다

interface Todo {
  title: string;
  description: string;
  completed: boolean;
  createdAt: number;
}
 
type TodoPreview = Omit<Todo, "description">;
 
const todo: TodoPreview = {
  title: "Clean room",
  completed: false,
  createdAt: 1615544252770,
};

Exclude<UnionType, ExcludedMembers>

Exclude<UnionType, ExcludedMembers>는 unionType에서 해당 멤버를 뺀다.

type T0 = Exclude<"a" | "b" | "c", "c">;
console.log(typeof T0); // "a" | "b"

type T1 = Exclude<"a" | "b" | "c", "b" | "c">;
console.log(typeof T1); // "a"

type T3 = Exclude<string | nubmer | (() => void), Function>;
console.log(typeof T3); // string | number

Extract<Type, Union>

Extract<Type, Union>는 겹치는 것만 남긴다.

type T0 = Extract<"a" | "b" | "c", "a" | "f">;
console.log(typeof T0); // "a"

type T1 = Extract<string | number | (() => void), Function>;
console.log(typeof T1); // () => void

0개의 댓글