Skip to content

Commit d231085

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

File tree

2 files changed

+43
-95
lines changed

2 files changed

+43
-95
lines changed

โ€Žsrc/index.tsโ€Ž

Lines changed: 8 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,10 @@
1-
import { json } from "react-router-dom";
2-
import { JoinUser, User } from "./utils/data.interface";
1+
//intersection types
32

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";
94

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+
}

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

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,47 @@ interface Toy {
3131
name: string;
3232
}
3333

34+
// ๋™์‹œ ํ™•์žฅ ๋ฐฉ์‹
3435
interface ToyCar extends Car, Toy {
3536
price: number;
3637
}
3738

38-
export interface User {
39+
type Job = "police" | "developer" | "teacher";
40+
41+
export interface UserName {
3942
name: string;
43+
job: Job;
44+
}
45+
46+
// '|' ์ด๊ฒŒ ์œ ๋‹ˆ์˜จ ํƒ€์ž…
47+
export interface HighSchoolStudent {
48+
name: number | string;
49+
grade: 1 | 2 | 3;
50+
}
51+
52+
// union types
53+
54+
export interface UnionCar {
55+
name: 'car';
56+
color: string;
57+
start(): void;
4058
}
4159

42-
export interface JoinUser {
60+
export interface UnionMobile {
61+
name: 'mobile';
62+
color: string;
63+
call(): void;
64+
}
65+
66+
//intersection types
67+
68+
export interface InterCar {
4369
name: string;
44-
age: number;
70+
start(): void;
71+
}
72+
73+
export interface InterToy {
74+
name: string;
75+
color: string;
76+
price: number;
4577
}

0 commit comments

Comments
ย (0)