Quick Start Typescript ~ 7์žฅ ์ •๋ฆฌ

์นดํƒˆ๋กœ๊ทธ
  1. 1. 7์žฅ ํด๋ž˜์Šค์™€ ์ธํ„ฐํŽ˜์ด์Šค ๋ชฉ์ฐจ
    1. 1.1. ๐Ÿ“183p. ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ง€์›
    2. 1.2. ๐Ÿ“p187. ๊ธฐ์กดJs(prototype) vs class ๋น„๊ต
    3. 1.3. ๐Ÿ“p188. ์ƒ์†(extends), ํฌํ•จ(2) ๊ด€๊ณ„
      1. 1.3.1. ํฌํ•จ ๊ด€๊ณ„
        1. 1.3.1.1. ํ•ฉ์„ฑ
        2. 1.3.1.2. ์ง‘ํ•ฉ
    4. 1.4. ๐Ÿ“p192. ์ ‘๊ทผ ์ œํ•œ์ž
    5. 1.5. ๐Ÿ“p195. ์ถ•์•ฝ ์ฝ”๋”ฉ๊ธฐ๋ฒ•
    6. 1.6. ๐Ÿ“p195. ์ ‘๊ทผ ์ œํ•œ์ž ์„ ์–ธ > ํด๋ž˜์Šค ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋จ
    7. 1.7. ๐Ÿ“p197. get/set ์ฝ”๋”ฉ์Šคํƒ€์ผ
    8. 1.8. ๐Ÿ“p197. ๋ถ€๋ชจ ํด๋ž˜์Šค ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ ์ด์šฉ
    9. 1.9. ๐Ÿ“p199. ๊ธฐ๋ณธ ์ ‘๊ทผ ์ œํ•œ์ž
    10. 1.10. ๐Ÿ“p202. ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•œ ๊ณตํ†ต ๊ธฐ๋Šฅ ์ •์˜
    11. 1.11. ๐Ÿ“ p205. Interface ๋‹ค์ค‘ ์ƒ์†
    12. 1.12. ๐Ÿ“p212. ํด๋ž˜์Šค๋ฅผ ๋ฐฐ์—ด ์š”์†Œ๋กœ ๋ณด๊ณ  ๋ฐฐ์—ด ํƒ€์ž… ์„ ์–ธ
    13. 1.13. ๐Ÿ“p215. ์ธํ„ฐํŽ˜์ด์Šค์— ํ•จ์ˆ˜ ํƒ€์ž… ์ •์˜
    14. 1.14. ๐Ÿ“p216. ์˜ค๋ฒ„๋ผ์ด๋”ฉ
    15. 1.15. ๐Ÿ“p219. ์˜ค๋ฒ„๋กœ๋”ฉ
    16. 1.16. ๐Ÿ“p222. ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํด๋ž˜์Šค์—์„œ ๊ตฌํ˜„ํ•˜์—ฌ ์˜ค๋ฒ„๋กœ๋”ฉ
    17. 1.17. ๐Ÿ“p224. ๋‹คํ˜•์„ฑ
      1. 1.17.1. ์ข…๋ฅ˜
      2. 1.17.2. ํด๋ž˜์Šค์˜ ๋‹คํ˜•์„ฑ
      3. 1.17.3. ์ธํ„ฐํŽ˜์ด์Šค์˜ ๋‹คํ˜•์„ฑ
      4. 1.17.4. ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๋‹คํ˜•์„ฑ (์œ ๋‹ˆ์–ธ ํƒ€์ž…)
        1. 1.17.4.1. ๋ฌธ์ œ์ 
      5. 1.17.5. โ˜…โ˜… ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๋‹คํ˜•์„ฑ (์ธํ„ฐํŽ˜์ด์Šค)
    18. 1.18. ๐Ÿ“p236. getter/setter ์‚ฌ์šฉ ์ด์œ 
      1. 1.18.1. ES5 ๋ณ€ํ™˜์‹œ
    19. 1.19. ๐Ÿ“p240. static
    20. 1.20. ๐Ÿ“p242. ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด
      1. 1.20.1. Eager Initalization
      2. 1.20.2. Lazy Initalization
    21. 1.21. ๐Ÿ“p247. readonly vs const
    22. 1.22. ๐Ÿ“p250. readonly ์ œ๊ฑฐ๋˜๋Š” ๊ฒฝ์šฐ

๐Ÿ“– Quick Start Typescript ์„ ์ฝ๊ณ , ๊ฐ„๋‹จํžˆ ๋ชฐ๋ž๋˜ ๋ถ€๋ถ„์ด๋‚˜ ์ค‘์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š” ๋ถ€๋ถ„์„ ์ž‘์„ฑ

๋ถ„๋Ÿ‰์ด ๋งŽ์•„, ์ด๋ฏธ ์•„๋Š” ๋‚ด์šฉ์ด๋‚˜ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๋ถ€๋ถ„์€ ์ƒ๋žตํ•จ

7์žฅ ํด๋ž˜์Šค์™€ ์ธํ„ฐํŽ˜์ด์Šค ๋ชฉ์ฐจ

๐Ÿ“183p. ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ง€์›

๊ฐ์ฒด์ง€ํ–ฅ์˜ ๋ชฉ์ ์€ ์ฝ”๋“œ ์ค‘๋ณต์„ ์ตœ์†Œํ™”

ES6์—์„œ class ํ‚ค์›Œ๋“œ๊ฐ€ ์ถ”๊ฐ€๋˜๋ฉด์„œ ๋ถ€์กฑํ•œ ์ ์ด ์กด์žฌ

๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์š”์†Œ Js(ES6) Ts
ํด๋ž˜์Šค class class
์ธํ„ฐํŽ˜์ด์Šค โ˜…์ง€์›์•ˆํ•จ interface
์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„ โ˜…์ง€์›์•ˆํ•จ implements
์ƒ์† extends extends
์ƒ์„ฑ์ž constructor(){} constructor(){}
์ ‘๊ทผ ์ œํ•œ์ž โ˜…์ง€์›์•ˆํ•จ private, public, protected
final ์ œํ•œ์ž โ˜…์ง€์›์•ˆํ•จ readonly(Ts 2.0๋ถ€ํ„ฐ)
static ํ‚ค์›Œ๋“œ static static
super ํ‚ค์›Œ๋“œ super super

์ œํ•œ์ž์˜ ๊ฒฝ์šฐ, ์‹ค์ œ Js๋กœ ๋ณ€ํ™˜๋ฌ์„ ๊ฒฝ์šฐ, ์‚ฌ๋ผ์ง€๋Š” ๋ถ€๋ถ„์œผ๋กœ ๊ฐœ๋ฐœ์ƒ์—์„œ๋งŒ ์ œํ•œ์˜ ์˜๋ฏธ๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ด ์•„์‰ฝ๋‹ค

๐Ÿ“p187. ๊ธฐ์กดJs(prototype) vs class ๋น„๊ต

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// ๊ธฐ์กด Js Prototype ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ
var Rectangle = (function() {
function Rectangle(x, y) {
this.x = x;
this.y = y;
}

Rectangle.prototype.getArea = function() {
return this.x * this.y;
};
return Rectangle;
})();

var rectangle = new Rectangle(1, 5);
var area = rectangle.getArea();
console.log(area);

๋ชจ๋“ˆ ํŒจํ„ด์€ ํด๋กœ์ €๋ฅผ ์ด์šฉํ•ด ๋น„๊ณต๊ฐœ๋œ ๋‚ด๋ถ€ ๋ฉ”์†Œ๋“œ๋ฅผ ์บก์Šํ™”ํ•˜์—ฌ, ์ „์—ญ๊ณต๊ฐ„์„ ๋”๋Ÿฝํžˆ์ง€ ์•Š๋Š” ์žฅ์  ์กด์žฌ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// class ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ

interface Rectangle {
x: number;
y: number;
getArea(): number;
}

class Rectanlge {
x: numberl
y: number;

constructor(x: number, y: number) {
this.x = x;
this.y = y;
}

getArea(): number {
return this.x * this.y;
}
}

๐Ÿ“p188. ์ƒ์†(extends), ํฌํ•จ(2) ๊ด€๊ณ„

์ƒ์† ์ƒ๋žต

ํฌํ•จ ๊ด€๊ณ„

  1. ํ•ฉ์„ฑ(composition) - ๊ฐ•ํ•œ๊ด€๊ณ„
  2. ์ง‘ํ•ฉ(aggregation) - ์•ฝํ•œ๊ด€๊ณ„

ํ•ฉ์„ฑ

1
2
3
4
5
6
7
8
9
10
class Engine{}
class Car {
private Engine;
constructor() {
this.engine = new Engine(); // ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
}
}

let myCar = new Car();
myCar = null; // null์ด ๋˜๋ฉด ํฌํ•จ๋œ ํด๋ž˜์Šค ํ•จ๊ป˜ ์ œ๊ฑฐ

์ง‘ํ•ฉ

1
2
3
4
5
6
7
8
9
10
class Engine{}
class Car {
private engine: Engine;
constructor(engine: Engine) {
this.engine = engine; // ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
}
}

let engine = new Engine(); // engine ์ธ์Šคํ„ด์Šค ๋ณ„๋„๋กœ ์„ ์–ธ
let car = new Car(engine); // ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ์‹œ, ํฌํ•จ๋˜๋Š” ํด๋ž˜์Šค๋ฅผ ๊ฐ™์ด ์ „๋‹ฌ

์œ„ ์ฝ”๋“œ(ํ•ฉ์„ฑ)๊ณผ์˜ ์ฐจ์ด์ ์€ ์ง‘ํ•ฉ์€ car ๊ฐ์ฒด๊ฐ€ ์ œ๊ฑฐ๋˜๋”๋ผ๋„,
engine ๊ฐ์ฒด๋Š” ์™ธ๋ถ€์—์„œ ์„ ์–ธ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ œ๊ฑฐ๋˜์ง€์•Š์Œ (์ˆ˜๋ช…์ฃผ๊ธฐ๋ฅผ ํ•จ๊ป˜ํ•˜์ง€ ์•Š๊ธฐ๋•Œ๋ฌธ์— ์•ฝํ•œ๊ด€๊ณ„)

์‹ค๋ฌด์—์„œ๋Š” ์–ด๋””์— ์ฃผ๋กœ ์‚ฌ์šฉ๋ ๊นŒ?

๐Ÿ“p192. ์ ‘๊ทผ ์ œํ•œ์ž

์ ‘๊ทผ ์ œํ•œ์ž ํŠน์ง• ์ƒ์† ์—ฌ๋ถ€ ์™ธ๋ถ€ ๊ฐ์ฒด ์ ‘๊ทผ
public ์™ธ๋ถ€ ๋˜๋Š” ์ž์‹ํด๋ž˜์Šค์—์„œ ์ ‘๊ทผ ๊ฐ€๋Šฅ O O
protected ์ž์‹ ํด๋ž˜์Šค์—์„œ ์ ‘๊ทผ ๊ฐ€๋Šฅ O X
private ํ•ด๋‹น ํด๋ž˜์Šค์—์„œ๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅ X X

๐Ÿ“p195. ์ถ•์•ฝ ์ฝ”๋”ฉ๊ธฐ๋ฒ•

์ฑ•ํ„ฐ์™€ ์ƒ๊ด€์—†์ง€๋งŒ, ๊ด€๋ จ ์žˆ๋Š” ๋ณ€์ˆ˜ ๋ฌถ์–ด์„œ ์„ ์–ธํ•˜๊ธฐ ์ข‹์•„๋ณด์—ฌ์„œ ๊ธฐ๋ก

1
let [cWidth, cLength, cHeight] = [1, 2, 3];

๐Ÿ“p195. ์ ‘๊ทผ ์ œํ•œ์ž ์„ ์–ธ > ํด๋ž˜์Šค ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋จ

1
2
3
4
5
6
7
8
9
class Cube {
constructor(public width: number) {}

getWidth() {
return this.width;
}
}
let cube = new Cube(6);
console.log(cube.width); // 6

๐Ÿ“p197. get/set ์ฝ”๋”ฉ์Šคํƒ€์ผ

๋งค๊ฐœ๋ณ€์ˆ˜/getter/setter ํ•œ๊บผ๋ฒˆ์— ๋ชจ์•„์„œ ๊ด€๋ฆฌ

1
2
3
4
5
6
7
8
9
class PC {
ram = `0G`;
get ramCapcity() {
return this.ram;
}
set ramCapcity(value) {
this.ram = value;
}
}

๐Ÿ“p197. ๋ถ€๋ชจ ํด๋ž˜์Šค ๋ฉค๋ฒ„ ๋ณ€์ˆ˜ ์ด์šฉ

super() ํ‚ค์›Œ๋“œ์™€ this ์‚ฌ์šฉ

super๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๊ณต๊ฐœ ๋ฉค๋ฒ„(public)์—๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅ

this๋Š” ๋ถ€๋ชจ์—๊ฒŒ ์ƒ์†๋ฐ›์€ ๋ฉค๋ฒ„์™€ ํ˜„์žฌ ํด๋ž˜์Šค ๋ชจ๋‘ ์ ‘๊ทผ ๊ฐ€๋Šฅ

๐Ÿ“p199. ๊ธฐ๋ณธ ์ ‘๊ทผ ์ œํ•œ์ž

์ž˜๋ชป ์•Œ๊ณ  ์žˆ๋˜ ์‚ฌํ•ญ

constructor ๋งค๊ฐœ๋ณ€์ˆ˜์—์„œ ์ ‘๊ทผ ์ œํ•œ์ž๋ฅผ ์„ค์ • ์•ˆํ• ์‹œ, default๊ฐ€ public์ผ์ค„ ์•Œ์•˜๋Š”๋ฐ private ์ž„

์ ‘๊ทผ ์ œํ•œ์ž๋ฅผ ์ƒ๋žตํ•  ๊ฒฝ์šฐ, ์ƒ์„ฑ์ž ์™ธ๋ถ€์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†์Œ!

image-20200406023739559

๐Ÿ“p202. ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•œ ๊ณตํ†ต ๊ธฐ๋Šฅ ์ •์˜

์–ธ์ œ ์‚ฌ์šฉ๋ ๊นŒ? ์‹ค๋ฌด์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ์ผ€์ด์Šค๊ฐ€ ๊ถ๊ธˆ

๊ตฌํ˜„ ๋ฉ”์†Œ๋“œ๋Š” ์‹ค์ œ ๊ตฌํ˜„ ๋‚ด์šฉ์„ ํฌํ•จ

์ถ”์ƒ ๋ฉ”์†Œ๋“œ๋Š” ์„ ์–ธ๋งŒ๋œ ๋ฉ”์†Œ๋“œ, ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ž์‹ ํด๋ž˜์Šค์—์„œ ์ถ”์ƒ ๋ฉ”์†Œ๋“œ๋ฅผ ๋ฐ›์•„ ๋ณ„๋„ ๊ตฌํ˜„ํ•ด์•ผํ•จ

! ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋‚˜ ์ถ”์ƒ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋Š” ์ž์‹ ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉ(overriding)ํ•  ์ˆ˜ ์žˆ๊ฒŒ public์œผ๋กœ ์„ ์–ธํ•ด์•ผํ•จ

์ถ”์ƒ ํด๋ž˜์Šค์— ๊ธฐ๋ฐ˜์€ ๋‘” ๊ตฌํ˜„ ๋ฐฉ์‹์€ ํ…œํ”Œ๋ฆฟ ๋ฉ”์„œ๋“œ ํŒจํ„ด์œผ๋กœ ๋งŽ์ด ์•Œ๋ ค์ง

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
abstract class AbstractBird {
abstract name: string;

abstract flySound(sound: string);

// ๊ตฌํ˜„ ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ์–ด๋„ ์ƒ๊ด€์ด ์—†๋‚˜๋ณด๋„ค
fly(): void {
this.flySound(`${this.name}: ํŒŒ๋‹ฅ`);
}
}

class RealBird extends AbstractBird {
constructor(public name: string) {
super();
}

// ์˜ค๋ฒ„๋ผ์ด๋”ฉ
flySound(sound: string) {
console.log(`${this.name} ๋‚ ์•„๊ฐ`);
}
}

let dogBird = new RealBird(`์ƒˆ`);
dogBird.fly(); // ์ƒˆ ๋‚ ์•„๊ฐ

๐Ÿ“ p205. Interface ๋‹ค์ค‘ ์ƒ์†

๋ชฐ๋ž๋˜ ๋ถ€๋ถ„

์ž์‹ ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์—ฌ๋Ÿฌ ๋ถ€๋ชจ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋‹ค์ค‘ ์ƒ์† ๊ฐ€๋Šฅ

1
2
3
4
5
6
7
8
9
10
11
interface Car { speed: number }
interface SportsCar { acceleration: number }

interface MyOptimizedCar extends Car, SportsCar {
waterproof: boolean;
}

let myCar = <MyOptimizedCar>{}; // ?? ์ธํ„ฐํŽ˜์ด์Šค๋งŒ์œผ๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”๊ฑด๊ฐ€?
myCar.speed = 100;
myCar.acceleration = 100;
myCar.waterproof = true;

! ๋งŒ์•ฝ ๋‹ค์ค‘ ์ƒ์† ๋ฐ›์„ ๋•Œ, ๊ฐ™์€ ์ด๋ฆ„์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์ƒ์†๋ฐ›์œผ๋ฉด ์žฌ์ •์˜ํ•ด์•ผํ•จ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
interface Dog {
run(): void;
getStatus(): {
runningSpeed: number;
};
}

interface Bird {
fly(): void;
getStatus(): {
flightSpeed: number;
};
}

interface DogBird extends Dog, Bird {
getStatus(): {
runningSpeed: number,
flightSpeed: number;
}
}

class NewAnimal implements DogBird {
run(): void {}
fly(): void {}
getStatus(): { runningSpeed: number, flightSpeed: number; } {
return {
runningSpeed: 10,
flightSpeed: 20
}
}
}

๐Ÿ“p212. ํด๋ž˜์Šค๋ฅผ ๋ฐฐ์—ด ์š”์†Œ๋กœ ๋ณด๊ณ  ๋ฐฐ์—ด ํƒ€์ž… ์„ ์–ธ

๋ชฐ๋ž๋˜ ๋ถ€๋ถ„

ํด๋ž˜์Šค ์ž์ฒด๋ฅผ ํƒ€์ž… ์„ ์–ธ ๋ถ€๋ถ„์— ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ

1
2
3
4
5
6
7
8
9
10
11
class Person {
public full: string;
constructor(public name: string, public city: string) {
this.full = name + `(${city})`;
}
}

let personArray: Person[] = [
new Person(`kim`, `name`),
new Person(`kang`, `name`)
];

๐Ÿ“p215. ์ธํ„ฐํŽ˜์ด์Šค์— ํ•จ์ˆ˜ ํƒ€์ž… ์ •์˜

์ต๋ช… ํ•จ์ˆ˜์— ๋Œ€ํ•œ ํ•จ์ˆ˜ ํƒ€์ž… ์ •์˜ ()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ •์˜ ํ•  ์ˆ˜ ์žˆ์Œ

! ๋งค๊ฐœ ๋ณ€์ˆ˜ ์ด๋ฆ„๊ณผ ํƒ€์ž…์ด ์ผ์น˜ํ•˜์ง€ ์•Š๋”๋ผ๋„ ์ƒ๊ด€์ด ์—†์Œ (??? ์ด์œ ๊ฐ€ ๋ญ˜๊นŒ)

1
2
3
4
5
6
7
8
9
10
11
interface IFormat {
(data: string, toUpper?: boolean): string;
}

let format: IFormat = function (data: string, toUpper: boolean) {
...
}

let format: IFormat = function (str: string, isUpper: boolean) {
...
}

๐Ÿ“p216. ์˜ค๋ฒ„๋ผ์ด๋”ฉ

์˜ค๋ฒ„๋ผ์ด๋”ฉ = ๋ถ€๋ชจ์—์„œ ์ƒ์†๋ฐ›์•„, ์ž์‹ ํด๋ž˜์Šค์—์„œ ์ƒˆ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•

๋‘ ๊ฐ€์ง€ ์กฐ๊ฑด ํ•„์š”

  1. ์กฐ๊ฑด1: ๋ถ€๋ชจํด๋ž˜์Šค์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ํƒ€์ž…์ด ๊ฐ™๊ฑฐ๋‚˜ ์ƒ์œ„ ํƒ€์ž…์ด์—ฌ์•ผํ•จ

  2. ์กฐ๊ฑด2: ๋ถ€๋ชจํด๋ž˜์Šค์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ™๊ฑฐ๋‚˜ ๋งŽ์•„์•ผ ํ•จ

๐Ÿ“p219. ์˜ค๋ฒ„๋กœ๋”ฉ

์˜ค๋ฒ„๋กœ๋”ฉ = ๋ฉ”์„œ๋“œ์˜ ์ด๋ฆ„์€ ๊ฐ™์ง€๋งŒ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ํƒ€์ž…๊ณผ ๊ฐœ์ˆ˜๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•

1
2
3
4
5
6
7
8
// ์ ์  ์ƒ์œ„์˜ ํƒ€์ž…์œผ๋กœ ์„ ์–ธ
typeCheck(value: number);
typeCheck(value: string);
typeCheck(value: any): void {
if (typeof value === `number`) console.log(`this is number`);
else if (typeof value === `string`) console.log(`this is string`);
else console.log(`nothing`);
}

๐Ÿ“p222. ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํด๋ž˜์Šค์—์„œ ๊ตฌํ˜„ํ•˜์—ฌ ์˜ค๋ฒ„๋กœ๋”ฉ

1
2
3
4
5
6
7
8
9
interface IPoint {
getX(x: any): any;
}

class Point implements IPoint {
getX(x?: number | string): any {
...
}
}

! ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ด์šฉํ•˜๋ฉด ์„ ์–ธ๊ณผ ๊ตฌํ˜„์„ ๋ถ„๋ฆฌํ•˜๊ณ  ๊ตฌํ˜„๋ถ€์˜ ๊ตฌ์กฐ๋ฅผ ๊ฐ•์ œ

์ด ์ ์—์„œ ๋กœ์ง๊ณผ ๊ตฌ์กฐ๊ฐ€ ์„ž์—ฌ ์žˆ๋Š” ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•ด ์˜ค๋ฒ„๋กœ๋”ฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค

๊ตฌ์กฐ๋งŒ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ณต์žก๊ณ  ๋‚ฎ์Šต๋‹ˆ๋‹ค.

๐Ÿ“p224. ๋‹คํ˜•์„ฑ

์ข…๋ฅ˜

  1. ํด๋ž˜์Šค์˜ ๋‹คํ˜•์„ฑ
  2. ์ธํ„ฐํŽ˜์ด์Šค์˜ ๋‹คํ˜•์„ฑ
  3. ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๋‹คํ˜•์„ฑ

ํด๋ž˜์Šค์˜ ๋‹คํ˜•์„ฑ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Planet {
stopTransduction(): void {
console.log(`stop - planet`);
}
}

class Earth extends Planet {
public features: string[] = [`soil`, `water`, `oxyzen`];

stopTransduction(): void {
console.log(`stop - earth`);
}

earthStop(): void {
console.log(`stop2 - earth`);
}
}

let earth: Planet = new Earth(); // โ˜… Earth ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ–ˆ์ง€๋งŒ, ํƒ€์ž…์€ ์ƒ์œ„์˜ Planet์ž„
earth.stopTransduction(); // stop - earth, ์ธ์Šคํ„ด์Šค์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉ
console.log(earth.features); // Error, ์ ‘๊ทผ ๋ถˆ๊ฐ€
earth.earthStop(); // Error, ์˜ค๋ฒ„๋ผ์ด๋”ฉ ๋˜์ง€ ์•Š์€ ๋ฉ”์†Œ๋“œ๋Š” ์ ‘๊ทผ ๋ถˆ๊ฐ€

๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ํƒ€์ž…์„ ์ง€์ •๋ฐ›์€ ์ธ์Šคํ„ด์Šค๋Š” ์‹ค์ œ ๋™์ž‘์€ ๋ถ€๋ชจ ํด๋ž˜์Šค ๊ธฐ์ค€์œผ๋กœ ์‹คํ–‰๋จ

๊ทธ๋ž˜์„œ ์ž์‹ ํด๋ž˜์Šค ๋ฉค๋ฒ„ ๋ณ€์ˆ˜(features)์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†์Œ

๊ทธ๋Ÿฌ๋‚˜, ๋ฉ”์†Œ๋“œ ์ž์ฒด๋Š” ์ž์‹ ์ธ์Šคํ„ด์Šค์˜ ๊ฒƒ์ด ์‹คํ–‰๋จ (๋Ÿฐํƒ€์ž„ ๋‹คํ˜•์„ฑ(runtime polymorphism)), ex. duck typing

์ธํ„ฐํŽ˜์ด์Šค์˜ ๋‹คํ˜•์„ฑ

ํด๋ž˜์Šค์™€ ๋‹ค๋ฅด์ง€ ์•Š์•„ ์ฝ”๋“œ๋งŒ ์ ๊ณ  ์ƒ๋žต

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
interface IPerson {
getAlias: () => string;
getAge(): number;
}

class PoliceMan implements IPerson {
getAlias = () => `happy`;

getAge(): number {
return 10;
}

hasClub() {
return true;
}
}

let policeMan: IPerson = new PoliceMan();
console.log(policeMan.hasClub()); // Error, ์ ‘๊ทผ ๋ถˆ๊ฐ€

๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๋‹คํ˜•์„ฑ (์œ ๋‹ˆ์–ธ ํƒ€์ž…)

1
display(data: string | number) {}

๋ฌธ์ œ์ 

ํƒ€์ž… ๊ฐ€๋“œ๊ฐ€ ๋นก์…ˆ

1
2
3
4
5
6
7
8
9
10
class MonitorDisplay {
display(monitor: Led | Oled | Uhd) {
if (monitor instanceof Led) {}
else if (monitor instanceof Oled) {}
else if (monitor instanceof Uhd) {
let myMonitor: Uhd = <Uhd>monitor;
return myMonitor.getName();
}
}
}

type ํ‚ค์›Œ๋“œ์‹œ ์ถ•์•ฝ์€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ๊ทผ๋ณธ์ ์ธ ํ•ด๊ฒฐ์ฑ…์€ ๋  ์ˆ˜ ์—†์Œ(ํด๋ž˜์Šค ํƒ€์ž… ์ถ”๊ฐ€์‹œ๋งˆ๋‹ค, ๋งค๋ฒˆ ์—…๋ฐ์ดํŠธ ํ•„์š”)

1
2
3
4
5
6
// ์—ฌ๊ธด ๋˜ ์‹ ๊ธฐํ•˜๊ฒŒ ๋Œ€๋ฌธ์ž๋กœ ๋ช…๋ช…ํ–ˆ๋„ค..
type MultiTypes = Led | Oled | Uhd;

class MonitorDisplay {
display(monitor: MultiTypes) { ...(if๋“ค) }
}

โ˜…โ˜… ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๋‹คํ˜•์„ฑ (์ธํ„ฐํŽ˜์ด์Šค)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
interface Monitor {
getName(): string;
}

class Led implements Monitor {
constructor(private name: string) {}
getName(): string {
return `LED: ` + this.name;
}
}

class Oled implements Monitor {
constructor(private name: string) {}
getName(): string {
return `Oled: ` + this.name;
}
}

class MonitorDisplay {
display(monitor: Monitor) {
let myMonitor: Monitor = monitor;
return myMonitor.getName();
}
}

if๋ฌธ ์—†์ด ์ฝ”๋”ฉ์ด ๊ฐ€๋Šฅํ•จ

๐Ÿ“p236. getter/setter ์‚ฌ์šฉ ์ด์œ 

๊ตณ์ด this.name = 'anything' ์ฒ˜๋Ÿผ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  get/set ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

๊ฐ’์„ ์„ค์ •ํ•˜๊ฑฐ๋‚˜ ์ฝ์„ ๋•Œ, ๋กœ์ง์„ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ(= ์กฐ๊ฑด ์ถ”๊ฐ€ ๊ฐ€๋Šฅ)

1
2
3
4
5
6
7
8
9
get name(): string {
return this.studentName;
}

set name(name: string) {
if (name.includes(`happy`)) {
this.studentName = name;
}
}

ES5 ๋ณ€ํ™˜์‹œ

Object.defineProperty ์ฐธ๊ณ 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Object.defineProperty(Student.prototype, `name`, {
get: function() {
return this.studentName;
},
set: function(name) {
if (name.includes(`happy`)) this.studentName = name;
},
enumerable: true, // true: ๊ฐ์ฒด ํ‚ค ์—ด๊ฑฐ ๊ฐ€๋Šฅ, default: false
configurable: true // true: ์ด ์†์„ฑ ๊ฐ’ ์ˆ˜์ •/์‚ญ์ œ ๊ฐ€๋Šฅ, default: false
})

for (var prop in Student.prototype) {
console.log(prop); // ์—ฌ๊ธฐ์— enumerable ํ•˜์ง€ ์•Š์œผ๋ฉด ํ‘œ์‹œ๋˜์ง€ ์•Š์Œ
}

๐Ÿ“p240. static

static ํ‚ค์›Œ๋“œ๋Š” ๊ฐ์ฒด ์ƒ์„ฑ ์—†์ด ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ๋ฉ”๋ชจ๋ฆฌ ์ ˆ์•ฝ ํšจ๊ณผ ์กด์žฌ

๊ฐ์ฒด ์ƒ์„ฑ ์—†์ด ๋ฐ”๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ

static ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋Š” ์ธ์Šคํ„ด์Šค๊ฐ„ ๊ฐ’ ๊ณต์œ 

1
2
3
4
5
6
7
8
9
10
11
12
13
class Circle {
static circleArea: number = 0;
get area(): number {
return Circle.circleArea; // this๊ฐ€ ์•„๋‹Œ Circle์„ ์‚ฌ์šฉํ–ˆ์Œ
}
static set area(pArea: number) {
Circle.circleArea = pArea;
}
}
Circle.area = 100;

let circle = new Circle();
console.log(circle.area); // 100, ๊ณต์œ ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธ

๐Ÿ“p242. ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด

static ํ‚ค์›Œ๋“œ๋ฅผ ํ™œ์šฉํ•˜๋ฉด ์œ ์ผํ•œ ์ƒํƒœ ์ •๋ณด ์ €์žฅ ๊ฐ€๋Šฅ

์ด๋ ‡๊ฒŒ ํ•˜๊ธฐ์œ„ํ•ด์„œ๋Š” ๊ฐ์ฒด ์ƒ์„ฑ์„ ๋ง‰๊ณ , ํด๋ž˜์Šค ๋ณ€์ˆ˜, ๋ฉ”์†Œ๋“œ ๋“ฑ ๋ชจ๋‘๋ฅผ static์œผ๋กœ ์„ ์–ธ

๋‹จ์ผ ์ƒํƒœ ๊ด€๋ฆฌ์—๋Š” ์ข‹์ง€๋งŒ, ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ์€ ๋ถˆ๊ฐ€๋Šฅ

์ƒ์„ฑ๋ฒ•

  1. ๋ถ€์ง€๋Ÿฐํ•œ ์ดˆ๊ธฐํ™” (eager initalization) - ํด๋ž˜์Šค ์„ ์–ธ์‹œ ์ดˆ๊ธฐํ™”
  2. ๊ฒŒ์œผ๋ฅธ ์ดˆ๊ธฐํ™” (lazy initalization) - ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ์‹œ ์ดˆ๊ธฐํ™”

Eager Initalization

1
2
3
4
5
6
7
8
9
10
11
class EagerLogger {
private static uniqueObj: EagerLogger = new EagerLogger(); // ๋‚ด๋ถ€์—์„œ ์ž์ฒด์  ์„ ์–ธ

// private๋ฅผ ๋ถ™์—ฌ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ๋ฐฉ์ง€
private EagerLogger() {}

// static์œผ๋กœ ์™ธ๋ถ€ ์ ‘๊ทผ ํ—ˆ์šฉ
public static getLogger(): EaggerLogger {
return this.uniqueObj;
}
}

Lazy Initalization

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class LazyLogger {
private static uniqueObj: LazyLogger;

private LazyLogger() {}

public static getLogger(): LazyLogger {
// ์ƒ์„ฑ๋œ ์ ์ด ์—†์œผ๋ฉด ์ƒˆ๋กœ ์ƒ์„ฑ, ํƒ€์ž…์€ LazyLogger์ด๋ฏ€๋กœ ==๋กœ ํƒ€์ž…๊ฒ€์‚ฌ ํ”ผํ•จ
if (this.uniqueObj == null) {
this.uniqueObj = new LazyLogger();
}

return this.uniqueObj;
}
}

๐Ÿ“p247. readonly vs const

const๊ฐ€ ์‚ฌ์šฉ๋˜๋Š” ๊ณณ์€ readonly๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•œ๋‹ค๊ณ  ์ดํ•ดํ•˜๋ฉด ํŽธํ•œ ๊ฒƒ ๊ฐ™๋‹ค

ํŠน์„ฑ const readonly
์ƒ์ˆ˜ ์„ ์–ธ ๊ฐ€๋Šฅ ๊ฐ€๋Šฅ
์ดˆ๊ธฐํ™” ๊ฐ•์ œ์„ฑ ํ•„์ˆ˜ ์„ ํƒ
๊ฐ’ ์žฌํ• ๋‹น ๋ถˆ๊ฐ€๋Šฅ ๊ฐ€๋Šฅ(?)
์„ ์–ธ ๊ฐ€๋Šฅ ๋Œ€์ƒ ๋ณ€์ˆ˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜
๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด
์ƒˆํƒ€์ž…
์„ ์–ธ ๋ถˆ๊ฐ€๋Šฅ ๋Œ€์ƒ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜
๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด
์ƒˆํƒ€์ž…
๋ณ€์ˆ˜
์‚ฌ์šฉ ์šฉ๋„ ์ƒ์ˆ˜ ์ฝ๊ธฐ ์ „์šฉ ์†์„ฑ
์ปดํŒŒ์ผ ์„ ์–ธ ์œ ์ง€ ์œ ์ง€ ์‚ฌ๋ผ์ง
์ง€์› ํ‘œ์ค€ ES6 TS 2.0

๐Ÿ“p250. readonly ์ œ๊ฑฐ๋˜๋Š” ๊ฒฝ์šฐ

type ์—์ผ๋ฆฌ์–ด์‹ฑ์‹œ ์‚ฌ๋ผ์ง

1
2
3
4
5
6
7
8
9
10
let emotion: { readonly name: string } = { name: `sad` };

function aliasing(pEmotion: { name: string }) {
pEmotion.name = `happy`;
}

console.log(emotion.name); // sad
emotion.name = `happy`; // Error
aliasing(emotion);
console.log(emotion.name); // happy, ๋ณ€๊ฒฝ๋จ