부끄럽지만 본인이 interface와 type을 무슨 기준을 두고 사용해야할지 명확하게 알지 못하는 것 같아 재정리
객체로 표현해야할 때: interface
특정 값만을 허용하는 매개변수 혹은 변수 혹은 함수 반환값: type
객체의 구조나 클래스 형태를 정의할 때 interface가 적합하다.
Interface의 장점
특정 값만 허용하는 유니온 타입이나 원시 타입 별칭에는 type이 더 적합하다.
Type의 장점
// ❌ 기존 코드 (객체로 정의했지만 실제로는 문자열만 필요)
interface ICategoryProps {
category: "group" | "intro" | "center";
}
// ✅ 개선된 코드 (특정 값만 허용하므로 type 사용)
type CategoryType = "group" | "intro" | "center";
// 객체 구조가 필요하다면 interface 사용
interface CategoryItem {
category: CategoryType; // type을 interface에서 활용
name: string;
description?: string;
}