1- import { Benz , Car } from "./utils/data.interface" ;
1+ import { json } from "react-router-dom" ;
2+ import { JoinUser , User } from "./utils/data.interface" ;
23
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 } ` ;
831 }
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 } ` ;
1243 }
1344}
1445
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