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
46 changes: 5 additions & 41 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,5 @@
//์ ‘๊ทผ ์ œํ•œ์ž(Access modifier) - public, private, protected / es6์—์„œ๋Š” ์ ‘๊ทผ ์ œํ•œ์ž๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์•˜๋‹ค ํ•˜์ง€๋งŒ ts์—์„œ๋Š” ์ œ๊ณตํ•ด์ค€๋‹ค.

abstract class Car {
color: string;
constructor(color: string) {
this.color = color;
}
start(): void {
console.log("start");
}
abstract doSomething(): string; //์•„๋ฌด ์ž‘์—…์„ ํ•˜์ง€ ์•Š์€ ์ถ”์ƒ ํ•จ์ˆ˜ ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค์— ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ์—†๋‹ค๋ฉด error ๋ฐœ์ƒ
}

// const Car = new Car("red"); //-> error : ์ถ”์ƒํด๋ž˜์Šค๋Š” new ์ธ์Šคํ„ด์Šค๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๋‹ค. ์ถ”์ƒํด๋ž˜์Šค๋Š” ์ฒญ์‚ฌ์ง„๊ณผ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉฐ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ์˜ค๋กœ์ง€ ์ƒ์†์„ ํ†ตํ•ด์„œ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค.

class Bmw extends Car {
constructor(color: string) {
super(color);
// ์ฐธ๊ณ ๋กœ 'super()'๋Š” ๋ถ€๋ชจ(์ผ๋ฐ˜์ ์ธ super๊ฐ€ ์•„๋‹˜)์˜ constructor์— ์ ‘๊ทผ
}
// abstract doSomething()๊ฐ€ ์—†๋‹ค๋ฉด class๋Š” error, ์ฆ‰, ์ถ”์ƒ ํด๋ž˜์Šค๋Š” ์ƒ์†์„ ๋ฐ›์€ ์ชฝ์—์„œ ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
doSomething(): string {
// alert("do!"); //-> error : alert๋Š” browser์—์„œ๋งŒ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅ
console.log("do!");
return 'Do!';
}
// - private: ๋ถ€๋ชจ name์ด private์ธ ๊ฒฝ์šฐ error๊ฐ€ ๋‚˜์˜จ๋‹ค. ์ถ”๊ฐ€์ ์œผ๋กœ '#name'์€ private๋กœ ์ธ์‹ํ•œ๋‹ค.
// - protected: ๋ถ€๋ชจ name์ด protected์ธ ๊ฒฝ์šฐ ์ •์ƒ ๋™์ž‘ํ•œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด public๊ณผ ์ฐจ์ด๋Š” ๋ฌด์—‡์ธ๊ฐ€.
/*
* new๋ฅผ ํ†ตํ•ด์„œ ์ธ์Šคํ„ด์Šค ํ–ˆ์„ ๊ฒฝ์šฐ ์ ‘๊ทผ์„ ์ œํ•œํ•˜๊ฒŒ ๋œ๋‹ค. ์ฆ‰, ์ž์‹ ํด๋ž˜์Šค์—์„œ๋Š” ๋ถ€๋ชจ ์†์„ฑ์— ์ ‘๊ทผ์„ ํ•  ์ˆ˜ ์žˆ์œผ๋‚˜ new ์ธ์Šคํ„ด์Šค์ธ ๊ฒฝ์šฐ ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€ํ•˜๋‹ค.
*/
// - readonly: new ์ธ์Šคํ„ด์Šค๋ฅผ ํ†ตํ•ด์„œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ๋ณ€๊ฒฝ์„ ํ•  ์ˆ˜ ์žˆ์„๊นŒ?
/*
* ๋ถ€๋ชจ constructor ๋‚ด๋ถ€์—์„œ ์ž‘์—…์„ ํ•ด์•ผํ•œ๋‹ค.
*/
// - static: static์€ ์ •์  ๋งด๋ฒ„ ๋ณ€์ˆ˜(property)๋ฅผ ๋งŒ๋“ค์–ด ์ค„ ์ˆ˜ ์žˆ๋‹ค. static์— ์ ‘๊ทผ์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” this๊ฐ€ ์•„๋‹Œ class ์ด๋ฆ„์œผ๋กœ ์ ‘๊ทผ์„ ํ•ด์•ผ ํ•œ๋‹ค.
// - abstract: abstract์€ ์ถ”์ƒ ํด๋ž˜์Šค(class) ๋˜๋Š” ํ•จ์ˆ˜, ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์ค€๋‹ค.
}

const z4 = new Bmw("black");
console.log(z4.doSomething()); //undefined๊ฐ€ ์ถœ๋ ฅ๋˜๋Š” ์ด์œ  : doSomething์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด void์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
// NonNullable<T> : NonNullable์€ Null๊ณผ undefined๋ฅผ ์ œ์™ธํ•œ ํƒ€์ž…์„ ์ƒ์„ฑํ•œ๋‹ค.
// -- data.interface ์ฐธ์กฐ
import {
GradeType, ScoreType, User
} from "./utils/data.interface"
24 changes: 23 additions & 1 deletion src/utils/data.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,26 @@ export interface InterToy {
name: string;
color: string;
price: number;
}
}

export interface User {
id: number;
name: string;
age: number;
gender: "M" | "F"
}

export interface ScoreTemp {
'1': 'A' | 'B' | 'C' | 'D';
'2': 'A' | 'B' | 'C' | 'D';
'3': 'A' | 'B' | 'C' | 'D';
'4': 'A' | 'B' | 'C' | 'D';
}

export type GradeType = '1' | '2' | '3' | '4';
export type ScoreType = 'A' | 'B' | 'C' | 'D';

//NonNullable
export type T1 = string | null | undefined | void;
//T2๋Š” NonNullable์˜ ๋”ฐ๋ผ T1์˜ ์œ ๋‹ˆ์˜จ์œผ๋กœ ์ง€์ •๋œ null๊ณผ undefined๋ฅผ ์ œ์™ธํ•˜๊ณ  string๊ณผ void๋งŒ ๋‚จ๊ฒŒ ๋œ๋‹ค.
export type T2 = NonNullable<T1>;