Skip to content

Commit 51097be

Browse files
authored
๐Ÿ”€: Merge pull request #7 from Programming-Contents-List/lecture/4
๐Ÿ”€: main๋ธŒ๋ Œ์น˜๋ฅผ ์ตœ์‹ ํ™” ํ•˜๊ธฐ์œ„ํ•œ merge
2 parents 212dbfd + d375bba commit 51097be

File tree

2 files changed

+98
-30
lines changed

2 files changed

+98
-30
lines changed

โ€Žsrc/index.ts

Lines changed: 89 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,94 @@
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 ๊ฐ•์œผ ๋ถ€๋ถ„ ๋‹ค์‹œ ๋“ค์–ด๋ณผ ํ•„์š” ์žˆ์Œ.

โ€Žsrc/utils/data.interface.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export interface userType {
88
[grade: number]: Score;
99
}
1010

11-
//ํ•จ์ˆ˜ interface ์„ ์–ธ ๋ฐฉ์‹
1211
export interface Add {
1312
(num1: number, num2: number): number;
1413
}
@@ -17,8 +16,6 @@ export interface isAdult {
1716
(age: number): boolean;
1817
}
1918

20-
// implements
21-
2219
export interface Car {
2320
color: string;
2421
wheels: number;
@@ -34,7 +31,15 @@ interface Toy {
3431
name: string;
3532
}
3633

37-
// ๋™์‹œ ํ™•์žฅ ๋ฐฉ์‹
3834
interface ToyCar extends Car, Toy {
3935
price: number;
36+
}
37+
38+
export interface User {
39+
name: string;
40+
}
41+
42+
export interface JoinUser {
43+
name: string;
44+
age: number;
4045
}

0 commit comments

Comments
ย (0)