๐ย ์ฌ๋ฌ ํ์ ์ ํฉ์ณ์ ์ฌ์ฉํ๋ค.
union types๊ฐ ์ฐ์ฐ์ or์ ์๋ฏธ์๋ค๋ฉด, intersection types๋ and์ ์๋ฏธ๋ฅผ ๊ฐ๋๋ค.
โ๏ธย ์ ์ฉํ๊ธฐ
interface Car {
name: string;
start(): void;
}
interface Toy {
name: string;
color: string;
price: number;
}
โ
const toyCar: Toy & Car = {
name: "ํ์",
start() {},
}
Type ย { name: string; start(): void; }ย is not assignable to type ย Toy & Carย Type ย { name: string; start(): void; }ย is missing the following properties from type ย Toyย : ย color, price
Toy & Car๋ผ๋ ๊ต์ฐจํ์ ์ ์ฌ์ฉํ๋ ค๋ฉด,
Toy์ Car๋ผ๋ interface์ ์ ์๋์ด ์๋ ๋ชจ๋ type๋ค์ ์ ์ด์ผํ๋ค.
๋ฐ๋ผ์ ์์ type ์๋ฌ๋ Toy๋ผ๋ interface์ ์๋ color์ price์ ๋ํ ์ ์๊ฐ ๋น ์ก๋ค๋ ๋ฌธ๊ตฌ์ด๋ค.
โญ๏ธ
const toyCar: Toy & Car = {
name: "ํ์",
start() {},
color: "blue",
price: 1000,
}
name์ ๊ณตํต์ผ๋ก ์ ์๋ type์ด๋ฏ๋ก ํ๋ฒ๋ง ์ ์ํด๋ ๋๋ค.