Skip to content

Commit 5667db5

Browse files
committed
🚩: 교차 interface 활용 및 사용 방법
1 parent e6bd4e3 commit 5667db5

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/index.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { UnionCar, UnionMobile } from "./utils/data.interface";
1+
//intersection types
22

3-
//속성이 많아 진다면 if문보다는 switch문으로 작성하는게 더 가독성이 높다.
4-
function getGift(gift: UnionCar | UnionMobile) {
5-
console.log(gift.color);
6-
if (gift.name === "car") {
7-
gift.start();
8-
} else {
9-
gift.call();
10-
}
3+
import { InterCar, InterToy } from "./utils/data.interface";
4+
5+
const toyCar: InterToy & InterCar = {
6+
name: '타요',
7+
start() { },
8+
color: 'blue',
9+
price: 1000,
1110
}

src/utils/data.interface.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,17 @@ export interface UnionMobile {
6161
name: 'mobile';
6262
color: string;
6363
call(): void;
64+
}
65+
66+
//intersection types
67+
68+
export interface InterCar {
69+
name: string;
70+
start(): void;
71+
}
72+
73+
export interface InterToy {
74+
name: string;
75+
color: string;
76+
price: number;
6477
}

0 commit comments

Comments
 (0)