Skip to content

Commit e6bd4e3

Browse files
committed
🚩: Union Types의 활용과 조건문과의 활용
1 parent fcc78aa commit e6bd4e3

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/index.ts

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

3-
import { UserName } from "./utils/data.interface";
4-
5-
const userName1 = 'Bob';
6-
let userName2: string | number = 'Tom';
7-
userName2 = 3;
8-
9-
const user: UserName = {
10-
name: "Bob",
11-
job: "developer"
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+
}
1211
}

src/utils/data.interface.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,18 @@ export interface UserName {
4747
export interface HighSchoolStudent {
4848
name: number | string;
4949
grade: 1 | 2 | 3;
50+
}
51+
52+
// union types
53+
54+
export interface UnionCar {
55+
name: 'car';
56+
color: string;
57+
start(): void;
58+
}
59+
60+
export interface UnionMobile {
61+
name: 'mobile';
62+
color: string;
63+
call(): void;
5064
}

0 commit comments

Comments
 (0)