interface Samsung{
name:string;
phonenumber: number;
}
interface KIA{
name: string;
carnumber: number;
}
interface Apple{
name: string;
Applenumber: number;
}
var phone : Samsung | KIA | Apple;
function IsSamsung(typeProduct : Samsung | KIA | Apple): typeProduct is Samsung{ // 삼성이 아니라면 phone = KIA | Apple
return (typeProduct as Samsung).phonenumber !== undefined;
}
function IsKIA(typeProduct : Samsung | KIA | Apple): typeProduct is KIA{ // KIA 가 아니라면 Apple
return (typeProduct as KIA).carnumber !== undefined;
}
if(IsSamsung(phone)){
phone.phonenumber;
}else{
if(IsKIA(phone)){
phone.carnumber;
}
else{
phone.Applenumber;
}
}