시리즈 초반에 type과 interface에 대해 알아보았다. 이번 포스팅에서는 조
금 더 파헤쳐보자. 이 둘의 기능적인 차이점에 대해서 알아보자.
interface IFunction {
(x: number, y: number): number;
}
type TFunction = (x: number, y: number) => number;
함수에서의 선언은 이전에 살펴본 것과 같다.
type Name = string;type UnionType = string | number;interface 같은 경우는 객체 프로퍼티 안에 string | number로 선언해줄 수는 있다. 하지만 타입을 직접적으로 union이나 intersection 하는 방법은 type 밖에 없다.type TupleType = [number, string];interface IRectangle {
height: number;
}
interface IRectangle {
width: number;
}
let rectangle: IRectangle = {
height: 200,
width: 300,
};
IRectangle이라는 서로 다른 프로퍼티를 가지는 interface를 2개 선언했다. 이 타입으로 만들어진 변수는 2개의 프로퍼티를 모두 가지고 있어야 한다.