리터럴 타입
const userName = "Bob";
let userName:string = "Tom";
userName = 3;
let userName:string | number = "Tom";
userName = 3;
유니온타입
type Job = "police" | "developer" | "teacher";
interface User {
name : string;
job : Job;
}
const user : User = {
name : "Bob",
job: "police",
}
interface Car {
name: "car";
color: string;
start():void;
}
interface Mobile {
name: "mobile";
color: string;
call(): void;
}
function getGift(gift: Car | Mobile) {
console.log(gift.color)
gift.start()
}
function getGift(gift: Car | Mobile) {
console.log(gift.color)
if(gift.name === "car") {
gift.start()
}
else {
gift.call();
}
}
교차타입
interface Car {
name: string;
start(): void;
}
interface Toy {
name: string;
color: string;
price: number;
}
const toyCar: Toy & Car = {
name: "타요",
start(){},
color: "blue",
price: 1000,
}