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

์นดํƒˆ๋กœ๊ทธ
  1. 1. 8์žฅ ๋ชจ๋“ˆ
    1. 1.1. ๐Ÿ“253p. ๋ชจ๋“ˆ ํ•„์š”์„ฑ
      1. 1.1.1. ๋ชจ๋“ˆ๋Ÿฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ๋ฐ˜ ๊ณผ์ •
    2. 1.2. ๐Ÿ“254p. ๋‚ด๋ถ€ ๋ชจ๋“ˆ, ์™ธ๋ถ€ ๋ชจ๋“ˆ ์ฐจ์ด
    3. 1.3. ๐Ÿ“259p. Namespace
      1. 1.3.1. namespace = module
        1. 1.3.1.1. ํ‚ค์›Œ๋“œ ์ค‘๋ณต ์ด์œ 
    4. 1.4. ๐Ÿ“261p. ํ•œ ํŒŒ์ผ์— ์—ฌ๋Ÿฌ ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์„ ์–ธ
    5. 1.5. ๐Ÿ“263p. โ˜… ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์—ฌ๋Ÿฌ ํŒŒ์ผ์— ์„ ์–ธ
    6. 1.6. ๐Ÿ“268p. ๋„ค์ž„์ŠคํŽ˜์ด์Šค ๋ชจ๋“ˆ
      1. 1.6.1. ์‚ฌ์šฉ๋ฒ•
    7. 1.7. ๐Ÿ“271p. ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์ด๋ฆ„ ํ™•์žฅ
    8. 1.8. ๐Ÿ“274p. ๋ธŒ๋ผ์šฐ์ €์—์„œ ๋„ค์ž„์ŠคํŽ˜์ด์Šค ๋ชจ๋“ˆ ํ˜ธ์ถœ
    9. 1.9. ๐Ÿ“276p. ๋ชจ๋“ˆ ์‚ฌ์šฉ๋ฒ•
      1. 1.9.1. ๊ฐœ๋ณ„ export
      2. 1.9.2. ํ•จ๊ป˜ export
      3. 1.9.3. ๋ชจ๋‘ export
      4. 1.9.4. ๋ชจ๋“ˆ ์žฌ๋…ธ์ถœ
    10. 1.10. ๐Ÿ“281p. โ˜…๋„ค์ž„์ŠคํŽ˜์ด์Šค๋กœ ๊ฐ์‹ธ์„œ ์žฌ๋…ธ์ถœ
    11. 1.11. ๐Ÿ“283p. ๋””ํดํŠธ ๋ฌด๋ฒ•
      1. 1.11.1. export-equals, import-equals ๋ฌธ
      2. 1.11.2. 2
      3. 1.11.3. ์ฃผ์˜์‚ฌํ•ญ
    12. 1.12. ๐Ÿ“289 ~ 313p. ๋ชจ๋“ˆ์‹œ์Šคํ…œ ์ƒ๋žต
    13. 1.13. ์ฐธ๊ณ ๋งํฌ

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

8์žฅ ๋ชจ๋“ˆ

๐Ÿ“253p. ๋ชจ๋“ˆ ํ•„์š”์„ฑ

  1. ์œ ์ง€๋ณด์ˆ˜
  2. ์ „์—ญ ์Šค์ฝ”ํ”„ ์˜ค์—ผ ๋ฐฉ์ง€
  3. ์žฌ์‚ฌ์šฉ์„ฑ

๋ชจ๋“ˆ๋Ÿฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ๋ฐ˜ ๊ณผ์ •

  1. ๋ชจ๋“ˆ ์‹๋ณ„
  2. ๋ชจ๋“ˆ ๋ถ„๋ฆฌ ์„ ์–ธ
  3. ์™ธ๋ถ€ ๊ณต๊ฐœ

๐Ÿ“254p. ๋‚ด๋ถ€ ๋ชจ๋“ˆ, ์™ธ๋ถ€ ๋ชจ๋“ˆ ์ฐจ์ด

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ 1.5๋ถ€ํ„ฐ Namespce๋ผ๋Š” ํŠน์ง•๊ณผ ES6 ๋ชจ๋“ˆ ํŠน์ง€์ด ์ถ”๊ฐ€ ECMAScript ํ‘œ์ค€ ์šฉ์–ด์ง‘์— 2๊ฐ€์ง€ ํ˜•ํƒœ์˜ ๋ชจ๋“ˆ ๊ตฌ๋ถ„

  1. ๋‚ด๋ถ€ ๋ชจ๋“ˆ - namespace
  2. ์™ธ๋ถ€ ๋ชจ๋“ˆ - export

๋‚ด๋ถ€๋ชจ๋“ˆ, namespace๋ž€ ์—ฌ๋Ÿฌ ํŒŒ์ผ์— ๊ฑธ์ณ ํ•˜๋‚˜์˜ ์ด๋ฆ„์„ ๊ณต์œ , reference๋ฅผ ํ†ตํ•ด ์ฐธ์กฐ

์™ธ๋ถ€๋ชจ๋“ˆ์€ ํŒŒ์ผ๋งˆ๋‹ค ์ด๋ฆ„ ๊ณต๊ฐ„์ด ์ •ํ•ด์ง, import๋ฅผ ํ†ตํ•ด ์ฐธ์กฐ

๊ทธ๋Ÿฌ๋ฏ€๋กœ, namespace๋Š” ํ”„๋กœ์ ํŠธ์™€ ๋ถ„๋ฆฌํ•ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‹จ์œ„์˜ ๋ชจ๋“ˆ์„ ๊ตฌ์„ฑํ•  ๋•Œ ์ข‹์Œ

@types ํด๋”์—์„œ ๊ตฌ๋ถ„ํ•˜์—ฌ ํƒ€์ž…์„ ์–ธ ๋“ฑ ์„ ํ•˜๋Š” ๋“ฏ ํ•˜๋‹ค

๐Ÿ“259p. Namespace

1
namespace Hello {}

namespace = module

ํ‚ค์›Œ๋“œ๋Š” ๋‹ค๋ฅด์ง€๋งŒ, ์—ญํ• ๊ณผ ๊ธฐ๋Šฅ์ƒ ์ฐจ์ด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค

ํ‚ค์›Œ๋“œ ์ค‘๋ณต ์ด์œ 

ES2015์—์„œ namespace ์šฉ์–ด๊ฐ€ ํ‘œ์ค€์œผ๋กœ ์ฑ„ํƒ๋˜๋ฉด์„œ, ์›๋ž˜ Typescript 1.5์—์„œ ์‚ฌ์šฉํ•˜๋˜ module ์šฉ์–ด๊ฐ€ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ Deprecated๋จ

๊ทธ๋Ÿฐ๋ฐ, Typescript ํƒ€์ž… ๋งŒ๋“œ๋Š” example code๋ฅผ ๋ณด๋ฉด ์•„์ง์€ module์ด๋ผ๋Š” ์šฉ์–ด๊ฐ€ ๋งŽ์ด ์“ฐ์ด๋Š” ๋“ฏ ํ•˜๋‹ค

์ฐธ๊ณ ๋งํฌ - chart.js

์—ฌ๊ธฐ๋Š” ์™œ module๊ณผ namespace๋ฅผ ๋™์‹œ์— ์‚ฌ์šฉํ•œ๊ฑธ๊นŒ?

1
2
3
4
5
6
7
8
9
// namespace ์‚ฌ์šฉ ์˜ˆ์‹œ
namespace Hello {
function print() {}
}

// module ์‚ฌ์šฉ ์˜ˆ์‹œ
module Hello {
function print() {}
}

ES6 ๋ณ€ํ™˜ ๊ฒฐ๊ณผ

1
2
3
4
var Hello;
(function: Hello) {
funtion print() {}
}(Hello || (Hello = {})); // ๋ชจ๋“ˆ์ด ์žˆ์œผ๋ฉด ์ „๋‹ฌ, ์—†์œผ๋ฉด ์ดˆ๊ธฐํ™” = ๋Š์Šจํ•œ ํ™•์žฅ loose argument

๐Ÿ“261p. ํ•œ ํŒŒ์ผ์— ์—ฌ๋Ÿฌ ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์„ ์–ธ

๋„ค์ž„์ŠคํŽ˜์ด์Šค๋งˆ๋‹ค ๊ตฌ๋ถ„์ด ํ•„์š”ํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ, export ์„ ์–ธ ํ•„์š”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
namespace MyInfo1 {
export let name = `name1`;
export function getName() {
return MyInfo2.name;
}
}

namespace MyInfo2 {
export let name = `name2`;
export function getName() {
return MyInfo1.name;
}
}

console.log(MyInfo1.getName());
console.log(MyInfo2.getName());

๋ณ€ํ™˜์‹œ var๋กœ ๋ณ€ํ™˜๋˜์–ด ํ˜ธ์ด์ŠคํŒ… ํŠน์„ฑ๋•Œ๋ฌธ์— ์ˆœ์„œ์™€ ์ƒ๊ด€์—†์ด ์„œ๋กœ ํ˜ธ์ถœ ๊ฐ€๋Šฅ

๐Ÿ“263p. โ˜… ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์—ฌ๋Ÿฌ ํŒŒ์ผ์— ์„ ์–ธ

ํ”„๋กœ์ ํŠธ ๊ทœ๋ชจ ์ปค์ง€๋ฉด, ํŒŒ์ผ ๋‹จ์œ„๋กœ ๋ชจ๋“ˆ์„ ๋ถ„ํ• 

tsc ๋ช…๋ น์‹œ, ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์ž๋™์œผ๋กœ ๋„ค์ž„์ŠคํŽ˜์ด์Šค๊ฐ„ ์ฐธ์กฐ ๊ด€๊ณ„๋ฅผ ๊ณ ๋ คํ•จ

๊ทธ๋Ÿฌ๋‚˜, ๊ฐœ๋ณ„ ํŒŒ์ผ์„ ์ปดํŒŒ์ผ์‹œ์—๋Š” ///<reference path="to/path" />๊ฐ€ ํ•„์š”ํ•จ

ํŒŒ์ผ ์ƒ๋‹จ์˜ ํ‘œ์‹œํ•˜๋ฉด ๋จ

๊ทธ๋Ÿฌ๋‚˜, ์‚ฌ์‹ค tsc ๋ช…๋ น์–ด๋กœ๋„ ๊ฐ™์ด

1
2
3
ts-node car2.ts # undefined, undefined ์ถœ๋ ฅ

tsc --out out.js car2.ts # ํ•ฉ์ณ ์ปดํŒŒ์ผ ํ•„์š”
ts-node car2.ts tsc -out out.js car2.ts
image-20200413001659451 image-20200413001720503

์ปดํŒŒ์ผ ํ›„์—๋„, ๊ฒฐ๊ณผ๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ํ‘œ์‹œ๋˜๊ฒŒ ํ•˜๋ ค๋ฉด, ๋„ค์ž„์ŠคํŽ˜์ด์Šค๋ฅผ ๋ชจ๋“ˆ๋กœ ์„ ์–ธํ•˜๊ณ  import๋ฅผ ์„ ์–ธ ํ•„์š”

(๋‹ค์Œ์žฅ์—์„œ ์„ค๋ช…๋จ)

๐Ÿ“268p. ๋„ค์ž„์ŠคํŽ˜์ด์Šค ๋ชจ๋“ˆ

์‚ฌ์šฉ๋ฒ•

1
2
3
4
5
6
7
8
// car1.ts
export namespace Car {
export let auto: boolean = false;
export interface ICar {
name: string;
vendor: string;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
import * as ns from './car1';

// namespace๋ฅผ ํ•œ๋ฒˆ ๋” ์„ ์–ธํ•ด์„œ ์‚ฌ์šฉ? ์ด์œ ๊ฐ€ ์žˆ๋‚˜?
namespace Car {
console.log(ns.Car.auto);

class Taxi implements ns.Car.ICar {
name: string;
vendor: string;
}
}

console.log(ns.Car.auto);

๐Ÿ“271p. ๋„ค์ž„์ŠคํŽ˜์ด์Šค ์ด๋ฆ„ ํ™•์žฅ

๋„ค์ž„์ŠคํŽ˜์ด์Šค ์ด๋ฆ„์€ .์„ ํ—ˆ์šฉ, ์ด๋ฆ„ ๊ณ„์ธต ํ™•์žฅํ•˜๋Š”๋ฐ ์‚ฌ์šฉ

์‚ฌ์šฉ ์˜ˆ์‹œ

1
2
3
// ์ˆœ์„œ ๋ฐ”๋€Œ์–ด๋„ ๋ฌธ์ œ๋Š” ์—†์ง€๋งŒ, ์ƒ์œ„์—์„œ ํ•˜์œ„๋กœ ์„ ์–ธํ•˜๋Š”๊ฒŒ ๋งž์Œ
namespace Animal{}
namespace Animal.Pet{}

๊ทธ๋Ÿฌ๋‚˜ ์ƒ์†์— ๊ฐœ๋…์ด ์ „ํ˜€ ์•„๋‹ˆ๊ณ , ์„œ๋กœ ๋‹ค๋ฅธ ๋„ค์ž„์ŠคํŽ˜์ด์Šค์ด๋ฏ€๋กœ, ๋ณ€์ˆ˜๋‚˜ ๋ฉ”์†Œ๋“œ ๊ณต์œ  X

๐Ÿ“274p. ๋ธŒ๋ผ์šฐ์ €์—์„œ ๋„ค์ž„์ŠคํŽ˜์ด์Šค ๋ชจ๋“ˆ ํ˜ธ์ถœ

๋„ค์ž„์ŠคํŽ˜์ด์Šค๊ฐ„ ๊ฒฐ๊ตญ js์—๋Š” ์—†๋Š” ๊ฐœ๋…์œผ๋กœ, ์ˆœ์„œ๋Œ€๋กœ js ์Šคํฌ๋ฆฝํŠธ ํ˜ธ์ถœํ•ด์„œ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค๋Š” ๊ฒƒ ๊ฐ™์Œ

๐Ÿ“276p. ๋ชจ๋“ˆ ์‚ฌ์šฉ๋ฒ•

๊ฐœ๋ณ„ export

1
2
export interface ICar {}
export function Test() {}
1
import { ICar, Test } from '../to/path';

ํ•จ๊ป˜ export

1
2
3
4
let ver: string = `1.0`;
let display = () => `hello`;

export { ver, display };
1
import { ver, display } from '../to/path';

๋ชจ๋‘ export

1
2
export let ver: string = `1.0`;
export let display = () => `hello`;
1
2
3
4
import * as m from './to/path'

console.log(m.ver);
console.log(m.display());

๋ชจ๋“ˆ ์žฌ๋…ธ์ถœ

1
2
export * from '../to/path';
export * from '../to/path';
1
import * as m from './to/path';

๋ชจ๋“ˆ ํŒŒ์ผ์„ ๊ฐ€์ ธ์™€์„œ ๋‹ค์‹œ export ํ•˜๋Š” ์˜ˆ์‹œ, ์ตœ์ƒ์œ„์— ๋ชจ๋“ˆ์—์„œ ๋งŽ์ด ์‚ฌ์šฉ๋จ

๐Ÿ“281p. โ˜…๋„ค์ž„์ŠคํŽ˜์ด์Šค๋กœ ๊ฐ์‹ธ์„œ ์žฌ๋…ธ์ถœ

namepsace ๊ฐ„ class๋ณด๋‹ค ์ƒ์œ„์˜ ๊ฐœ๋…์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ์‹ธ์„œ ๋…ธ์ถœํ•˜๋ฉด ํŽธํ•œ ๊ฒƒ ๊ฐ™๋‹ค

1
2
3
4
// car-info.module.ts <- ๋ชจ๋“ˆ ํŒŒ์ผ ๋ช…๋ช…๋ฒ•
export namespace CarInfo {
export function hello() {}
}
1
2
3
import { CarInfo } from './car-info.module';

CarInfo.hello();

๐Ÿ“283p. ๋””ํดํŠธ ๋ฌด๋ฒ•

export-equals, import-equals ๋ฌธ

default ์ด์ „์— ์‚ฌ์šฉ๋ฌ๋˜ ๋ฐฉ์‹์ธ ๊ฒƒ ๊ฐ™์€๋ฐ, ๋ชฐ๋ž๋˜ ๋ถ€๋ถ„ - ๊ฐ€๋” ๋‹ค๋ฅธ ํ”„๋กœ์ ํŠธ์—์„œ ์ข…์ข… ๋ณด์˜€๋Š”๋ฐ ์ด๋Ÿฐ ์˜๋ฏธ์˜€๊ตฌ๋‚˜

1
2
3
4
5
6
export = Chart;

// ๋™์ผ

export default Chart;
export { Chart as default };

2

1
2
3
4
5
import Validator from './validator';

// ๋™์ผ

import { default as Validator } from './validator';

์ฃผ์˜์‚ฌํ•ญ

๋ชจ๋“ˆ ํ•˜๋‚˜๋‹น default๋Š” ํ•˜๋‚˜๋งŒ ์„ ์–ธ ๊ฐ€๋Šฅ

๐Ÿ“289 ~ 313p. ๋ชจ๋“ˆ์‹œ์Šคํ…œ ์ƒ๋žต

๋ชจ๋“ˆ ์‹œ์Šคํ…œ์€ ๊ฐ ํŒŒํŠธ๋งˆ๋‹ค ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์›น๊ฐœ๋ฐœ์ž๋ผ๋ฉด ES2015๋ชจ๋“ˆ์„, Node.js๊ฐœ๋ฐœ์ž๋ผ๋ฉด CommonJs ๋ชจ๋“ˆ ํ˜•์‹์„ ๊ธฐ๋ณธ์œผ๋กœ ์•Œ๊ณ , ๋‚˜๋จธ์ง€๋Š” ์ƒํ™ฉ์— ๋”ฐ๋ผ ์•Œ์•„์•ผํ•  ๊ฒƒ ๊ฐ™๋‹ค

์ฐธ๊ณ ๋งํฌ

์ฑ…์€ ์ด๋ก ์ ์ธ ๋ถ€๋ถ„์œผ๋กœ ๋งŽ์ด ์„ค๋ช…๋˜์–ด์žˆ์–ด, ์‹ค์ œ์—์„œ๋Š” ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉ๋˜๊ณ , ๋” ์ถ”๊ฐ€์ ์ธ ๋‚ด์šฉ์„ ์ฐพ์•„๋ด„