[TIL] 20210929

Jihyun·2021년 9월 28일
0

interface 와 type aliases

  • interface는 어떤 것을 구현하겠다는 약속
  • type aliases는 data를 담을 목적일 때 사용
  • interface는 똑같은 interface를 또 정의하면 확장이 된다(type은 안됨)
  • type은 computed properties를 사용할 수 있다!

Mapped Types

(https://www.typescriptlang.org/docs/handbook/2/mapped-types.html)
JavaScript에서 배열에 map을 이용하는 것처럼 type에서도 map을 이용한 것과 같이 적용할 수 있다.

// 위 링크에서 발췌
type OnlyBoolsAndHorses = {
  [key: string]: boolean | Horse;
};
 
const conforms: OnlyBoolsAndHorses = {
  del: true,
  rodney: false,
};

Conditional Types

(https://www.typescriptlang.org/docs/handbook/2/conditional-types.html)
조건을 줄 수도 있다.

interface Animal {
  live(): void;
}
interface Dog extends Animal {
  woof(): void;
}
 
type Example = Dog extends Animal ? number : string;

Utility Types

https://www.typescriptlang.org/docs/handbook/utility-types.html)
타입스크립트가 제공하는 몇 가지 유틸 타입이 있다.

  • Partial : 해당 type의 모든 key를 Optional하게 만듬
  • Required : Partial의 반대. 모두 반드시 필요하게 만듬
  • Readonly : 해당 type의 모든 값을 변경할 수 없도록 만듬
  • Pick : 해당 type의 일부만 선택하도록 함 (Pick<Person, "name" | "age">)
  • Omit : Pick의 반대. 빼고싶은 key를 선택하도록 함

이외에도 많은 유틸 타입이 있다.
(꼭 한번씩 사용해보기⭐)

profile
Don't dream it, be it.

2개의 댓글

comment-user-thumbnail
2021년 9월 29일

멋져멋져

1개의 답글