typescript에서 객체의 타입"만" 을 지정해주기 위한 키워드이다.
... 끝이다 그러면 왜 type을 냅두고 interface를 쓰는걸까
type도 객체의 모양을 지정 해줄 수 있다.
interface mart{
carrot : string
apple : string
banana : string
storeNum : number
}
type mart = {
carrot : string
apple : string
banana : string
storeNum : number
}
이 코드들은 서로 같은 의미이지만
type mart = string;
이코드는 사용가능
interface mart = string;
이코드로는 사용 할 수 없다.
범용성으로는 type이 더 많은 기능을 할 수 있다.
ex)
type ChampionItme = {
championStartItem: "DoranRing" | "Droanshiled" | "LongSword" | "Highman's_5_red_potion"
}
type NativeOfChampion = "Demacia" | "Noxus" | "Ionia"
type Champian = {
name : string;
skill : string;
ultimate : boolean;
native : NativeOfChampion;
}
const irelia : Champian ={
name : "ilelia",
skill : "qqqqq",
ultimate : true,
native : "Ionia"
}
const darius : ChampionItme ={
championStartItem : "LongSword"
}
이렇게 무지성 type 보다는
interface ChampionItme{
championStartItem: "DoranRing" | "Droanshiled" | "LongSword" | "Highman's_5_red_potion"
}
type NativeOfChampion = "Demacia" | "Noxus" | "Ionia"
interface Champian{
name : string;
skill : string;
ultimate : boolean;
native : NativeOfChampion;
}
const irelia : Champian ={
name : "ilelia",
skill : "qqqqq",
ultimate : true,
native : "Ionia"
}
const darius : ChampionItme ={
championStartItem : "LongSword"
}
처럼 구분 지어지는게 가독성면에서 좋을것이다.
interface는 객체의 모양을 나타낼때만 사용된다.
type은 객체, 변수등 여러방면에서 사용된다.
이에 비슷한 개념은 var 에서 let 과 const 로 분리되듯,
그냥 써도 무방하지만 최소한의 안전장치로써, 코드의 가독성을 위해 분리해놓은 것으로 생각된다.
interface = 객체의 타입
라는 생각으로 Interface라는 키워드를 보자마자 객체에 관련된
타입이라고 알 수 있드시 말이다.