TIL. Interfaces vs Type Aliases

chloe·2021년 3월 19일
0

Typescript

목록 보기
5/8
post-thumbnail

Type Aliases와 interface 차이점

type aliases는 interface처럼 동작한다! 하지만 몇가지 차이가 있다.

1번째 차이점 ) interface는 여러 곳에서 사용되는 새로운 이름(new name)을 만들지만, type aliases는 새로운 이름을 만들지 않는다.

type Alias = { num: number }

interface Interface {
  num: number;
}

2번째 차이점) type aliases는 extend되거나 implement될 수 없다.

소프트웨어의 이상적인 속성이 확장에 용이하다는 것이기에 가능하다면 type alias보다 interface를 이용해야 한다.
그러나, interface로 표현할 수 없는 형태이고 union,tuple을 이용해야 한다면, type alias를 이용한다

Type Aliases와 interface 공통점

타입에 대한 이름 지어줄 수 있음!
Type Alias와 Interface 둘 다 타입에 대해 비슷한 방식으로 이름을 지어줄 수 있다.

interface Human{
  name:string;
  age:number;
}
const chloe:Human={
  name:"보아",
  age:22,
};

//

type Human={
  name:string;
  age:number;
};

const chloe:Human={
  name:"보아",
  age:20,
};

참고:https://medium.com/@alexsung/typescript-type%EA%B3%BC-interface-%EC%B0%A8%EC%9D%B4-86666e3e90c

profile
Front-end Developer 👩🏻‍💻

0개의 댓글