File tree Expand file tree Collapse file tree 2 files changed +23
-10
lines changed
Expand file tree Collapse file tree 2 files changed +23
-10
lines changed Original file line number Diff line number Diff line change 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}
Original file line number Diff line number Diff line change @@ -47,4 +47,18 @@ export interface UserName {
4747export 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}
You can’t perform that action at this time.
0 commit comments