둘 다 데이터의 shape를 기술하며 흔히 교환 가능하지만, 서로 다른 기능을 가지고 있습니다.
ts
interface User { name: string; age: number; }
type User2 = { name: string; age: number; };
둘 다 데이터의 shape를 기술하며 흔히 교환 가능하지만, 서로 다른 기능을 가지고 있습니다.
interface User { name: string; age: number; }
type User2 = { name: string; age: number; };
type이 할 수 있고 가 할 수 없는 것interfacetype ID = string | number; // union
type Pair = [number, number]; // tuple
type Name = User["name"]; // indexed/mapped/conditional 타입
type Nullable<T> = T | null; // 임의의 타입을 감쌈
type은 어떤 타입이든 가리키는 일반적인 **별칭(alias)**입니다 — 원시 타입, union, tuple, mapped type 등. interface는 객체/함수 shape만 기술합니다.
interface가 할 수 있고 type이 할 수 없는 것interface Box { width: number; }
interface Box { height: number; } // declaration merging — 둘이 하나로 병합됨
// 이제 Box는 width와 height를 모두 가짐
Interface는 declaration merging(여러 선언이 결합됨)을 지원하며, 서드파티 라이브러리 타입의 보강(augment)을 포함해 확장/보강의 관용적인 방식입니다.
interface Admin extends User { role: string; } // interface
type Admin2 = User & { role: string }; // type은 intersection 사용
흔한 관례: 객체 shape와 공개 API에는 **interface**를 사용하고(더 나은 오류 메시지, 확장 가능, 병합 가능), union·tuple·기타 타입 연산이 필요할 때는 **type**을 사용합니다. 일관성을 위해 하나를 기본값으로 정하세요 — 많은 팀이 객체에는 interface를 기본으로 하고 추가 기능이 필요할 때만 type에 손을 뻗습니다.