1
- import { Benz , Car } from "./utils/data.interface" ;
1
+ import { json } from "react-router-dom" ;
2
+ import { JoinUser , User } from "./utils/data.interface" ;
2
3
3
- //ํด๋์ค : ๊ฐ์ฒด์ ํ๋๊ณผ ๊ตฌ์กฐ๋ฅผ ์ ์ํ๊ธฐ์ '='์ ์ฌ์ฉํ๋ค.
4
- class Bmw implements Car {
5
- color = '' ;
6
- constructor ( color : string ) {
7
- this . color = color ;
4
+ // ํจ์
5
+ function add ( num1 : number , num2 : number ) {
6
+ // return num1 + num2;
7
+ console . log ( num1 + num2 ) ;
8
+ }
9
+
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 } ` ;
8
31
}
9
- wheels = 4 ;
10
- start ( ) {
11
- console . log ( 'go..' ) ;
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 } ` ;
12
43
}
13
44
}
14
45
15
- const b = new Bmw ( 'white' ) ;
16
- console . log ( b ) ;
17
- b . start ( ) ;
18
-
19
- //extends
20
- //๊ฐ์ฒด ๋ฆฌํฐ๋ด : ๊ฐ์ฒด๋ฅผ ๋ฆฌํฐ๋ดํด์ผํ๊ธฐ ๋๋ฌธ์ key, value ๊ฐ์ผ๋ก ์ ์ํ๊ฒ ๋๊ณ ํด๋์ค์ ๋ฌ๋ฆฌ '='๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
21
- const benz : Benz = {
22
- color : 'black' ,
23
- wheels : 4 ,
24
- start ( ) {
25
- console . log ( 'go...' ) ;
26
- } ,
27
- door : 5 ,
28
- stop ( ) {
29
- console . log ( 'stop...' ) ;
30
- } ,
31
- }
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 ๊ฐ์ผ ๋ถ๋ถ ๋ค์ ๋ค์ด๋ณผ ํ์ ์์.
0 commit comments