1
- import { json } from "react-router-dom" ;
2
- import { JoinUser , User } from "./utils/data.interface" ;
1
+ //intersection types
3
2
4
- // ํจ์
5
- function add ( num1 : number , num2 : number ) {
6
- // return num1 + num2;
7
- console . log ( num1 + num2 ) ;
8
- }
3
+ import { InterCar , InterToy } from "./utils/data.interface" ;
9
4
10
- function isAdult ( age : number ) : boolean {
11
- return age > 19 ;
12
- }
13
-
14
- // '?' : ์ต์
๋ ํ๋ผ๋ฉํฐ - ์ ํ์ ๋งค๊ฐ๋ณ์
15
- function hello ( name ?: string ) {
16
- return `Hello, ${ name || "world" } ` ;
17
- }
18
-
19
- const result = hello ( ) ;
20
- const result2 = hello ( 'sam' ) ;
21
-
22
- function hello2 ( name = 'world' ) {
23
- return `Hello, ${ name } ` ;
24
- }
25
- // ์ต์
๋ ํ๋ผ๋ฉํฐ ํน์ง - ์ต์
๋ ํ๋ผ๋ฉํฐ๋ ํญ์ ๋ค์ ์์ด์ผํ๋ค. ์ฆ, name ์์ age?๊ฐ ์์ด์๋ ์๋๋ค.
26
- function userInfo ( name : string , age ?: number ) : string {
27
- if ( age !== undefined ) {
28
- return `Hello, ${ name } . You are ${ age } .` ;
29
- } else {
30
- return `Hello, ${ name } ` ;
31
- }
32
- }
33
-
34
- console . log ( userInfo ( "sam" ) ) ;
35
- console . log ( userInfo ( "sam" , 30 ) ) ;
36
-
37
- // ์ต์
๋ ํ๋ผ๋งคํฐ๋ฅผ ์์ ๋๊ณ ์ถ๋ค๋ฉด undefined๋ฅผ ์ ๋์จ ๋ฐฉ์์ผ๋ก ์ฌ์ฉํด์ผ ํ๋ค.
38
- function userInfo2 ( age : number | undefined , name : string ) : string {
39
- if ( age !== undefined ) {
40
- return `Hello, ${ name } . You are ${ age } .` ;
41
- } else {
42
- return `Hello, ${ name } ` ;
43
- }
44
- }
45
-
46
- console . log ( userInfo2 ( 30 , "sam" ) ) ;
47
- console . log ( userInfo2 ( undefined , "sam" ) ) ;
48
-
49
- //๋๋จธ์ง ์ฐ์ฐ ํจ์
50
- //spread ๋ฐฉ์์ ๋ฐฐ์ด๋ก ๊ฐ์ด ๋ฆฌํด ๋๊ธฐ ๋๋ฌธ์ ๋ฐฐ์ด ํ์
์ ์ง์ ํด์ผ ํ๋ค.
51
- function addSpread ( ...nums : number [ ] ) {
52
- return nums . reduce ( ( result , num ) => result + num , 0 ) ;
53
- }
54
-
55
- addSpread ( 1 , 2 , 3 ) ; //6
56
- addSpread ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ) ; //55
57
-
58
- //bind ๋ฐฉ์
59
-
60
- const Sam : User = { name : 'Sam' }
61
-
62
- function showName ( this : User , age : number , gender : 'm' | 'f' ) { //this: User
63
- console . log ( this . name , age , gender ) ;
64
- }
65
-
66
- const a = showName . bind ( Sam ) ;
67
- a ( 30 , 'm' ) ;
68
-
69
- //
70
- function join ( name : string , age : number ) : JoinUser ; //overload
71
- function join ( name : string , age : string ) : string ; //overload
72
-
73
- function join ( name : string , age : number | string ) : JoinUser | string {
74
- if ( typeof age === "number" ) {
75
- //JoinUser ํ์
์ ๋ฆฌํด ๊ฐ
76
- return {
77
- name,
78
- age,
79
- } ;
80
- } else {
81
- //string ํ์
์ ๋ฆฌํด ๊ฐ
82
- return "๋์ด๋ ์ซ์๋ก ์
๋ ฅํด์ฃผ์ธ์." ;
83
- }
84
- }
85
- //์ฌ๊ธฐ์ error ๊ฐ ๋์ค๋ ์ด์ ๋ sam์ jane ๋ณ์๋ ํ์ ์ด ์๋ค. JoinUser ๋๋ string์ด ๋ฆฌํด ๊ฐ์ด๊ธฐ ๋๋ฌธ์ด๋ค.
86
- // const sam: JoinUser = join("Sam", 30); //error
87
- // const jane: string = join("Jane", "30"); //error
88
- //์ด๋ด ๋๋ overload๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
89
-
90
- //overload ์ ์ฉ ํ์ ๋์ log๊ฐ
91
- const sam : JoinUser = join ( "Sam" , 30 ) ;
92
- const jane : string = join ( "Jane" , "30" ) ;
93
-
94
- //overload ๊ฐ์ผ ๋ถ๋ถ ๋ค์ ๋ค์ด๋ณผ ํ์ ์์.
5
+ const toyCar : InterToy & InterCar = {
6
+ name : 'ํ์' ,
7
+ start ( ) { } ,
8
+ color : 'blue' ,
9
+ price : 1000 ,
10
+ }
0 commit comments