Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 89 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,94 @@
import { Benz, Car } from "./utils/data.interface";
import { json } from "react-router-dom";
import { JoinUser, User } from "./utils/data.interface";

//ํด๋ž˜์Šค : ๊ฐ์ฒด์˜ ํ–‰๋™๊ณผ ๊ตฌ์กฐ๋ฅผ ์ •์˜ํ•˜๊ธฐ์— '='์„ ์‚ฌ์šฉํ•œ๋‹ค.
class Bmw implements Car {
color = '';
constructor(color: string) {
this.color = color;
// ํ•จ์ˆ˜
function add(num1: number, num2: number) {
// return num1 + num2;
console.log(num1 + num2);
}

function isAdult(age: number): boolean {
return age > 19;
}

// '?' : ์˜ต์…”๋„ ํŒŒ๋ผ๋ฉ”ํ„ฐ - ์„ ํƒ์  ๋งค๊ฐœ๋ณ€์ˆ˜
function hello(name?: string) {
return `Hello, ${name || "world"}`;
}

const result = hello();
const result2 = hello('sam');

function hello2(name = 'world') {
return `Hello, ${name}`;
}
// ์˜ต์…”๋„ ํŒŒ๋ผ๋ฉ”ํ„ฐ ํŠน์ง• - ์˜ต์…”๋„ ํŒŒ๋ผ๋ฉ”ํ„ฐ๋Š” ํ•ญ์ƒ ๋’ค์— ์žˆ์–ด์•ผํ•œ๋‹ค. ์ฆ‰, name ์•ž์— age?๊ฐ€ ์žˆ์–ด์„œ๋Š” ์•ˆ๋œ๋‹ค.
function userInfo(name: string, age?: number): string {
if (age !== undefined) {
return `Hello, ${name}. You are ${age}.`;
} else {
return `Hello, ${name}`;
}
wheels = 4;
start() {
console.log('go..');
}

console.log(userInfo("sam"));
console.log(userInfo("sam", 30));

// ์˜ต์…”๋„ ํŒŒ๋ผ๋งคํ„ฐ๋ฅผ ์•ž์— ๋‘๊ณ  ์‹ถ๋‹ค๋ฉด undefined๋ฅผ ์œ ๋‹ˆ์˜จ ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
function userInfo2(age: number | undefined, name: string): string {
if (age !== undefined) {
return `Hello, ${name}. You are ${age}.`;
} else {
return `Hello, ${name}`;
}
}

const b = new Bmw('white');
console.log(b);
b.start();

//extends
//๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด : ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ฐ๋Ÿดํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— key, value ๊ฐ’์œผ๋กœ ์ •์˜ํ•˜๊ฒŒ ๋˜๊ณ  ํด๋ž˜์Šค์™€ ๋‹ฌ๋ฆฌ '='๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
const benz: Benz = {
color: 'black',
wheels: 4,
start() {
console.log('go...');
},
door: 5,
stop() {
console.log('stop...');
},
}
console.log(userInfo2(30, "sam"));
console.log(userInfo2(undefined, "sam"));

//๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ ํ•จ์ˆ˜
//spread ๋ฐฉ์‹์€ ๋ฐฐ์—ด๋กœ ๊ฐ’์ด ๋ฆฌํ„ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์—ด ํƒ€์ž…์„ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค.
function addSpread(...nums: number[]) {
return nums.reduce((result, num) => result + num, 0);
}

addSpread(1, 2, 3); //6
addSpread(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); //55

//bind ๋ฐฉ์‹

const Sam: User = { name: 'Sam' }

function showName(this: User, age: number, gender: 'm' | 'f') {//this: User
console.log(this.name, age, gender);
}

const a = showName.bind(Sam);
a(30, 'm');

//
function join(name: string, age: number): JoinUser; //overload
function join(name: string, age: string): string; //overload

function join(name: string, age: number | string): JoinUser | string {
if (typeof age === "number") {
//JoinUser ํƒ€์ž…์˜ ๋ฆฌํ„ด ๊ฐ’
return {
name,
age,
};
} else {
//string ํƒ€์ž…์˜ ๋ฆฌํ„ด ๊ฐ’
return "๋‚˜์ด๋Š” ์ˆซ์ž๋กœ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.";
}
}
//์—ฌ๊ธฐ์„œ error ๊ฐ€ ๋‚˜์˜ค๋Š” ์ด์œ ๋Š” sam์™€ jane ๋ณ€์ˆ˜๋Š” ํ™•์‹ ์ด ์—†๋‹ค. JoinUser ๋˜๋Š” string์ด ๋ฆฌํ„ด ๊ฐ’์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
// const sam: JoinUser = join("Sam", 30); //error
// const jane: string = join("Jane", "30"); //error
//์ด๋Ÿด ๋•Œ๋Š” overload๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

//overload ์ ์šฉ ํ–ˆ์„ ๋•Œ์˜ log๊ฐ’
const sam: JoinUser = join("Sam", 30);
const jane: string = join("Jane", "30");

//overload ๊ฐ•์œผ ๋ถ€๋ถ„ ๋‹ค์‹œ ๋“ค์–ด๋ณผ ํ•„์š” ์žˆ์Œ.
13 changes: 9 additions & 4 deletions src/utils/data.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export interface userType {
[grade: number]: Score;
}

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

// implements

export interface Car {
color: string;
wheels: number;
Expand All @@ -34,7 +31,15 @@ interface Toy {
name: string;
}

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

export interface User {
name: string;
}

export interface JoinUser {
name: string;
age: number;
}