ν™ˆ Namespace
포슀트
μ·¨μ†Œ

Namespace

πŸ’» Namespace λž€?

TypeScript μ—μ„œ μ½”λ“œλ₯Ό 논리적인 λ‹¨μœ„λ‘œ λ¬Άμ–΄μ£ΌλŠ” 역할을 ν•©λ‹ˆλ‹€.μ½”λ“œλ₯Ό λ¬Άμ–΄μ€ŒμœΌλ‘œμ¨, μΊ‘μŠν™” 및 λͺ¨λ“ˆν™”λ₯Ό μ μš©ν•  수 있고 이λ₯Ό 톡해 이름 좩돌(name collision)을 ν”Όν•  수 μžˆμŠ΅λ‹ˆλ‹€.
namespace λ₯Ό ν†΅ν•œ λͺ¨λ“ˆν™”λŠ” μ‹€ν–‰ ν™˜κ²½μ— λ”°λ₯Έ λͺ¨λ“ˆμ‹œμŠ€ν…œμ— 영ν–₯을 받지 μ•ŠμŠ΅λ‹ˆλ‹€. 즉 namespace λ₯Ό μ»΄νŒŒμΌν•œ JavaScript μ½”λ“œμ—λŠ” require/exports(module μ˜΅μ…˜μ΄ Node 인 경우)λ‚˜ import/export(module μ˜΅μ…˜μ΄ ES6 인 경우) 문이 ν¬ν•¨λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

πŸ’» Encapsulation

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
32
// Validators.ts
interface StringValidator {
  isAcceptable(s: string): boolean;
}

let lettersRegexp = /^[A-Za-z]+$/;
let numberRegexp = /^[0-9]+$/;

class LettersOnlyValidator implements StringValidator {
  isAcceptable(s: string) {
    return lettersRegexp.test(s);
  }
}

class ZipCodeValidator implements StringValidator {
  isAcceptable(s: string) {
    return s.length === 5 && numberRegexp.test(s);
  }
}

let strings = ["Hello", "98052", "101"];

let validators: { [s: string]: StringValidator } = {};
validators["Zip code"] = new ZipCodeValidator();
validators["Letters only"] = new LettersOnlyValidator();

for (let s of strings) {
  for (let name in validators) {
    let isMatch = validators[name].isAcceptable(s);
    console.log(`'${s}' ${isMatch ? "matches" : "does not match"} '${name}'.`);
  }
}

μœ„ Validators.ts νŒŒμΌμ€ script νŒŒμΌμ΄λ―€λ‘œ, λͺ¨λ“  λ³€μˆ˜λ₯Ό μ „μ—­μœΌλ‘œ μ°Έμ‘°ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
λ§Œμ•½ μ½”λ“œκ°€ ν”„λ‘œμ νŠΈμ˜ 규λͺ¨κ°€ 컀지면, μœ μ§€ λ³΄μˆ˜κ°€ μ–΄λ €μ›Œμ§€κ³  λ³€μˆ˜μ˜ 이름도 μΆ©λŒν•  수 있기 λ•Œλ¬Έμ— 논리적인 λ‹¨μœ„λ‘œ λ¬Άμ–΄ λͺ¨λ“ˆν™” μ‹œμΌœμ•Ό ν•©λ‹ˆλ‹€.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Validators.ts

// ...μƒλž΅...
namespace Validation {
  let lettersRegexp = /^[A-Za-z]+$/;
  let numberRegexp = /^[0-9]+$/;

  export class LettersOnlyValidator implements StringValidator {
    isAcceptable(s: string) {
      return lettersRegexp.test(s);
    }
  }

  export class ZipCodeValidator implements StringValidator {
    isAcceptable(s: string) {
      return s.length === 5 && numberRegexp.test(s);
    }
  }
}
// ...μƒλž΅...

Validation μ΄λΌλŠ” 이름을 가진 namespace 둜 κ²€μ¦λ‘œμ§ 및 μ •κ·œ ν‘œν˜„μ‹μ„ λͺ¨λ“ˆν™” ν–ˆμŠ΅λ‹ˆλ‹€. export λ˜μ§€ μ•Šμ€ μ •κ·œν‘œν˜„μ‹λ“€μ€ namespace μ™ΈλΆ€μ—μ„œ μ°Έμ‘°ν•  수 μ—†κ³ , export 된 class λ“€λ§Œ [namespace].[exported value] ν˜•μ‹μ„ μ‚¬μš©ν•˜μ—¬ μ™ΈλΆ€μ—μ„œ μ°Έμ‘°ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
λ”°λΌμ„œ κ²€μ¦λ‘œμ§μ„ μ‚¬μš©ν•˜λŠ” μ½”λ“œλ“€μ€ μ•„λž˜μ™€ 같이 λ³€κ²½λ˜μ–΄μ•Ό ν•©λ‹ˆλ‹€.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Validators.ts

// ...μƒλž΅...

// Validation.lettersRegexp -> Error
let strings = ["Hello", "98052", "101"];

let validators: { [s: string]: StringValidator } = {};
validators["Zip code"] = new Validation.ZipCodeValidator(); // λ³€κ²½
validators["Letters only"] = new Validation.LettersOnlyValidator(); // λ³€κ²½

for (let s of strings) {
  for (let name in validators) {
    let isMatch = validators[name].isAcceptable(s);
    console.log(`'${s}' ${isMatch ? "matches" : "does not match"} '${name}'.`);
  }
}

πŸ–Š namespace 컴파일

μœ„ μ½”λ“œμ—μ„œ namespace λ₯Ό μ μš©ν•œ 뢀뢄을 μ»΄νŒŒμΌν•œ 결과물은 μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var Validation;
(function (Validation) {
    let lettersRegexp = /^[A-Za-z]+$/;
    let numberRegexp = /^[0-9]+$/;
    class LettersOnlyValidator {
        isAcceptable(s) {
            return lettersRegexp.test(s);
        }
    }
    Validation.LettersOnlyValidator = LettersOnlyValidator;
    class ZipCodeValidator {
        isAcceptable(s) {
            return s.length === 5 && numberRegexp.test(s);
        }
    }
    Validation.ZipCodeValidator = ZipCodeValidator;
})(Validation || (Validation = {}));>

λͺ¨λ“ˆ μ‹œμŠ€ν…œμ„ μ‚¬μš©ν•˜μ§€ μ•Šκ³  IIFE λ₯Ό μ΄μš©ν•˜μ—¬ λͺ¨λ“ˆν™”λ₯Ό κ΅¬ν˜„ν–ˆμŠ΅λ‹ˆλ‹€.
이λ₯Ό 톡해, namespace λŠ” μ‹€ν–‰ ν™˜κ²½μ— λ”°λ₯Έ λͺ¨λ“ˆμ‹œμŠ€ν…œμ— 영ν–₯을 받지 μ•ŠλŠ”λ‹€λŠ” 것을 λ³Ό 수 μžˆμŠ΅λ‹ˆλ‹€.

πŸ’» Export Types & Namespace

νƒ€μž…μ΄λ‚˜ namespace 도 namespace μ•ˆμ— ν¬ν•¨λ˜μ–΄ export 될 수 μžˆμŠ΅λ‹ˆλ‹€.

1
2
3
4
5
6
7
8
9
10
11
12
// Validators.ts

namespace Validator {
  export interface StringValidator {
    isAcceptable(s: string): boolean;
  }
  // ...μƒλž΅...
}

// ...μƒλž΅...
let validators: { [s: string]: Validation.StringValidator } = {};
// ...μƒλž΅...

StringValidator μΈν„°νŽ˜μ΄μŠ€κ°€ namespace λ‚΄λΆ€μ—μ„œ export 되고 μžˆκΈ°λ•Œλ¬Έμ— Validator.StringValidator 둜 μ°Έμ‘°ν•˜κ³  μžˆμŒμ„ μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€.

Namespace 도 같은 λ°©μ‹μœΌλ‘œ namespace λ‚΄λΆ€μ—μ„œ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

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
namespace Validator {
  export namespace Types {
    export interface StringValidator {
      isAcceptable(s: string): boolean;
    }
  }

  let lettersRegexp = /^[A-Za-z]+$/;
  let numberRegexp = /^[0-9]+$/;

  export namespace Classes {
    export class LettersOnlyValidator implements Types.StringValidator {
      isAcceptable(s: string) {
        return lettersRegexp.test(s);
      }
    }

    export class ZipCodeValidator implements Types.StringValidator {
      isAcceptable(s: string) {
        return s.length === 5 && numberRegexp.test(s);
      }
    }
  }
}

// ...μƒλž΅...
let validators: { [s: string]: Validation.Types.StringValidator } = {};
validators["Zip code"] = new Validation.Classes.ZipCodeValidator();
validators["Letters only"] = new Validation.Classes.LettersOnlyValidator();
// ...μƒλž΅...

πŸ’» Alias

Namespace μ•ˆμ— namespace λ₯Ό 쀑첩해 μ‚¬μš©ν•˜λŠ” 경우, λ‚΄λΆ€ namespace 의 λ³€μˆ˜ μ°Έμ‘°μ‹œ μ½”λ“œκ°€ κΈΈμ–΄μ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€.
μœ„μ—μ„œ μ‚΄νŽ΄λ³Έ μ½”λ“œμ—μ„œ Validation.Types.StringValidator, Validation.Classes.ZipCodeValidator 와 Validation.Classes.LettersOnlyValidator κ°€ κ·Έ μ˜ˆμ‹œμž…λ‹ˆλ‹€.

Alias λ₯Ό 톡해 이 문제λ₯Ό ν•΄κ²°ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

1
2
3
4
5
6
7
8
9
10
11
12
// Validators.ts

// ...μƒλž΅...

import Types = Validator.Types;
import Classes = Validator.Classes;

let validators: { [s: string]: Types.StringValidator } = {};
validators["Zip code"] = new Classes.ZipCodeValidator();
validators["Letters only"] = new Classes.LettersOnlyValidator();

// ...μƒλž΅...

μœ„ μ˜ˆμ‹œμ²˜λŸΌ import q = x.y.z ν‘œν˜„μ‹μ„ μ‚¬μš©ν•˜λ©΄ alias λ₯Ό ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

Alias λ₯Ό ν•˜λ €λŠ” 것이 κ°’(value)이면, λ³€μˆ˜μ— ν• λ‹Ήν•΄ alias λ₯Ό ν•  수 있고, νƒ€μž…μ˜ 경우 type 에 ν• λ‹Ήν•΄ alias λ₯Ό ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

1
2
3
4
// ...μƒλž΅...
type Types = Validator.Types;
const Classes = Validator.Classes;
// ...μƒλž΅...

πŸ’» Multi-file namespaces

λ‘œμ§μ„ μž¬μ‚¬μš©ν•˜κΈ° μœ„ν•΄, 일반적으둜 νŒŒμΌμ„ 뢄리해 λͺ¨λ“ˆν™”μ‹œν‚¨ ν›„ ν•΄λ‹Ή νŒŒμΌλ‚΄λΆ€μ— μ—°κ΄€λœ λ‘œμ§μ„ μž‘μ„±ν•©λ‹ˆλ‹€. μœ„μ—μ„œ μ‚΄νŽ΄λ³Έ Validators.ts 파일의 namespace 도 λΆ„λ¦¬ν•΄λ³΄κ² μŠ΅λ‹ˆλ‹€.

1
2
3
4
5
6
7
// Validation.ts

export namespace Validation {
  export interface StringValidator {
    isAcceptable(s: string): boolean;
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
// LettersOnlyValidators.ts

import { Validation } from "./Validation";

export namespace LettersOnlyValidators {
  const lettersRegexp = /^[A-Za-z]+$/;
  export class LettersOnlyValidator implements Validation.StringValidator {
    isAcceptable(s: string) {
      return lettersRegexp.test(s);
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
// ZipCodeValidators.ts

import { Validation } from "./Validation";

export namespace ZipCodeValidators {
  const numberRegexp = /^[0-9]+$/;
  export class ZipCodeValidator implements Validation.StringValidator {
    isAcceptable(s: string) {
      return s.length === 5 && numberRegexp.test(s);
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Test.ts

import { Validation } from "./Validation";
import { LettersOnlyValidators } from "./LettersOnlyValidators";
import { ZipCodeValidators } from "./ZipCodeValidators";

let strings = ["Hello", "98052", "101"];

let validators: { [s: string]: Validation.StringValidator } = {};
validators["ZIP code"] = new ZipCodeValidators.ZipCodeValidator();
validators["Letters only"] = new LettersOnlyValidators.LettersOnlyValidator();

for (let s of strings) {
  for (let name in validators) {
    console.log(
      `"${s}" - ${
        validators[name].isAcceptable(s) ? "matches" : "does not match"
      } ${name}`
    );
  }
}

λΆ„λ¦¬λœ νŒŒμΌμ— μž‘μ„±λœ namespace λ₯Ό export ꡬ문으둜 내보내고, import ꡬ문으둜 κ°€μ Έμ˜€λŠ” 것을 λ³Ό 수 μžˆμŠ΅λ‹ˆλ‹€. ν•΄λ‹Ή 방식을 μ‚¬μš©ν•˜μ—¬ λͺ¨λ“ˆν™”λ₯Ό μ‹œν‚¬ 수 μžˆμ§€λ§Œ, νŠΉμ • ν™˜κ²½μ˜ λͺ¨λ“ˆ μ‹œμŠ€ν…œμ— μ˜μ‘΄μ„±μ΄ μƒκΈ΄λ‹€λŠ” λ¬Έμ œκ°€ λ°œμƒν•©λ‹ˆλ‹€.

πŸ’» Triple-Slash Directives

TypeScript μ—μ„œ μ œκ³΅ν•˜λŠ” Triple-Slash Directive λ₯Ό μ‚¬μš©ν•˜μ—¬ λͺ¨λ“ˆμ„ μž„ν¬νŠΈν•˜λ©΄ μœ„μ˜ λ¬Έμ œμ μ„ ν•΄κ²°ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

<reference path=”…”/ >

μœ„μ˜ μ§€μ‹œμ–΄λ₯Ό 파일의 μ΅œμƒλ‹¨μ— μœ„μΉ˜μ‹œμΌœ μ£Όλ©΄, path 에 λͺ…μ‹œλœ 경둜의 νŒŒμΌμ„ 컴파일 λ‹¨κ³„μ—μ„œ ν¬ν•¨μ‹œν‚΅λ‹ˆλ‹€.

Triple-Slash Directives λ₯Ό μ‚¬μš©ν•΄ μœ„μ˜ μ½”λ“œλ₯Ό μ•„λž˜μ™€ 같이 λ³€κ²½ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

1
2
3
4
5
6
7
// Validation.ts

export namespace Validation {
  export interface StringValidator {
    isAcceptable(s: string): boolean;
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
// LettersOnlyValidators.ts

/// <reference path="Validation.ts"/>
import { Validation } from "./Validation";

export namespace LettersOnlyValidators {
  const lettersRegexp = /^[A-Za-z]+$/;
  export class LettersOnlyValidator implements Validation.StringValidator {
    isAcceptable(s: string) {
      return lettersRegexp.test(s);
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
// ZipCodeValidators.ts

/// <reference path="Validation.ts"/>
namespace ZipCodeValidators {
  const numberRegexp = /^[0-9]+$/;
  export class ZipCodeValidator implements Validation.StringValidator {
    isAcceptable(s: string) {
      return s.length === 5 && numberRegexp.test(s);
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Test.ts

/// <reference path="Validation.ts"/>
/// <reference path="LettersOnlyValidators.ts"/>
/// <reference path="ZipCodeValidators.ts"/>
import { Validation } from "./Validation";
import { LettersOnlyValidators } from "./LettersOnlyValidators";
import { ZipCodeValidators } from "./ZipCodeValidators";

let strings = ["Hello", "98052", "101"];

let validators: { [s: string]: Validation.StringValidator } = {};
validators["ZIP code"] = new ZipCodeValidators.ZipCodeValidator();
validators["Letters only"] = new LettersOnlyValidators.LettersOnlyValidator();

for (let s of strings) {
  for (let name in validators) {
    console.log(
      `"${s}" - ${
        validators[name].isAcceptable(s) ? "matches" : "does not match"
      } ${name}`
    );
  }
}

μœ„ μ½”λ“œμ—μ„œ tsc Test.ts 을 μ‹€ν–‰μ‹œν‚€λ©΄, Test.ts 파일과 μ˜μ‘΄μ„±μ„ 가진 파일 λͺ¨λ‘ 컴파일 되게 λ©λ‹ˆλ‹€.

μ£Όμ˜ν•  점은, 컴파일된 Test.js νŒŒμΌμ€ μ‹€ν–‰ν•  수 μ—†μŠ΅λ‹ˆλ‹€. Tripe-Slash Directives λŠ” λͺ¨λ“ˆκ°„ μ˜μ‘΄μ„±μ„ λ‚˜νƒ€λ‚΄λŠ” 역할을 ν•  뿐, μ‹€μ œλ‘œ λͺ¨λ“ˆμ„ μž„ν¬νŠΈ ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€.
Test.js νŒŒμΌμ„ ν™˜κ²½λ³„λ‘œ μ‹€ν–‰ν•˜λŠ” 방법은 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

  • Node
    TSConfig 의 outFile μ˜΅μ…˜μ„ 지정해 μ»΄νŒŒμΌμ— ν¬ν•¨λœ λͺ¨λ“ˆλ“€μ„ ν•˜λ‚˜μ˜ 파일둜 ν•©μΉœν›„ μ‹€ν–‰ν•΄μ•Ό ν•©λ‹ˆλ‹€.
    νŒŒμΌμ„ ν•©μΉ λ•Œ μ»΄νŒŒμΌλŸ¬λŠ” reference νƒœκ·Έλ₯Ό 기반으둜 λͺ¨λ“ˆμ„ μžλ™μ •λ ¬ ν•©λ‹ˆλ‹€.

    > tsc Test.ts --outfile bundle.js
    > node Test.js

  • Browser
    script νƒœκ·Έμ— μ˜μ‘΄μ„±μ„ κ°–λŠ” μˆœμ„œλŒ€λ‘œ 컴파일된 λͺ¨λ“ˆλ“€μ„ λͺ…μ‹œν•΄ μ£Όκ±°λ‚˜, outFile μ˜΅μ…˜μ„ 지정해 ν•˜λ‚˜λ‘œ 합쳐진 νŒŒμΌμ„ λͺ…μ‹œν•΄ μ£Όμ–΄ μ‹€ν–‰μ‹œν‚¬ 수 μžˆμŠ΅λ‹ˆλ‹€.

πŸ–Š Triple-Slash Directives μ’…λ₯˜

  1. <reference path=”…”/ >
    ν•΄λ‹Ή μ§€μ‹œμ–΄λŠ” 파일 κ°„μ˜ μ˜μ‘΄μ„± μ„ μ–ΈμœΌλ‘œ μ‚¬μš©λ©λ‹ˆλ‹€. 이 μ§€μ‹œμ–΄κ°€ 파일의 μƒμœ„μ— μ‘΄μž¬ν•˜λ©΄ μ»΄νŒŒμΌλŸ¬λŠ” μΆ”κ°€ νŒŒμΌμ„ 컴파일 과정에 ν¬ν•¨μ‹œν‚΅λ‹ˆλ‹€.
    λ˜ν•œ μœ„μ—μ„œ μ‚΄νŽ΄λ³΄μ•˜λ“―μ΄, --outFile μ˜΅μ…˜μ„ μ‚¬μš©ν•  λ•Œ λͺ¨λ“ˆμ˜ μ •λ ¬ μˆœμ„œλ₯Ό μ§€μ •ν•˜λŠ” 역할도 ν•©λ‹ˆλ‹€.
  2. <reference types=”…”/ >
    νŒ¨ν‚€μ§€μ˜ μ˜μ‘΄μ„±μ„ μ„ μ–Έν•˜λŠ” μ§€μ‹œμ–΄μž…λ‹ˆλ‹€.
    Type declaration νŒŒμΌμ— <reference types=”node”/ > λ₯Ό μ„ μ–Έν•˜λŠ” 것은 @types/node/index.d.ts 에 μ„ μ–Έλœ νƒ€μž…μ„ μ‚¬μš©ν•œλ‹€κ³  μ•Œλ¦¬λŠ” 것이고, 이 νŒ¨ν‚€μ§€λŠ” μ„ μ–Έ 파일과 ν•¨κ»˜ μ»΄νŒŒμΌμ— ν¬ν•¨λ©λ‹ˆλ‹€.
  3. <reference lib=”…”/ >
    이 μ§€μ‹œμ–΄λŠ” 파일이 λͺ…μ‹œμ μœΌλ‘œ κΈ°μ‘΄ λ‚΄μž₯ lib λ₯Ό ν¬ν•¨ν•˜κ²Œ ν•©λ‹ˆλ‹€.
    TSConfig 의 lib μ˜΅μ…˜κ³Ό 같은 λ°©μ‹μœΌλ‘œ 지정이 λ©λ‹ˆλ‹€. λ˜ν•œ λ‚΄μž₯ νƒ€μž…μ— μ˜μ‘΄ν•˜λŠ” type declaration λ₯Ό μž‘μ„±ν• λ•ŒλŠ” ν•΄λ‹Ή μ§€μ‹œμ–΄λ₯Ό λͺ…μ‹œν•΄μ£ΌλŠ” 것이 ꢌμž₯λ©λ‹ˆλ‹€.

πŸ’» Namespace merging

Namespace λŠ” interface 처럼 같은 μ‹λ³„μžλ₯Ό 가진 것듀 끼리 병합(merging)이 λ©λ‹ˆλ‹€.

1
2
3
4
5
6
// Validation.ts
namespace Validation {
  export interface StringValidator {
    isAcceptable(s: string): boolean;
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
// LetterOnlyValidations.ts

/// <reference path="Validation.ts"/>

namespace Validation {
  const lettersRegexp = /^[A-Za-z]+$/;
  export class LettersOnlyValidator implements StringValidator {
    isAcceptable(s: string) {
      return lettersRegexp.test(s);
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
// ZipCodeValidators.ts

/// <reference path="Validation.ts" />
namespace Validation {
  const numberRegexp = /^[0-9]+$/;
  export class ZipCodeValidator implements StringValidator {
    isAcceptable(s: string) {
      return s.length === 5 && numberRegexp.test(s);
    }
  }
}

LetterOnlyValidations.ts νŒŒμΌμ€ Validation.ts 와 같은 μ‹λ³„μžμ˜ namespace λ₯Ό 가지고 있으며, Validation.ts λ₯Ό μ°Έμ‘°ν•˜κ³  μžˆμœΌλ―€λ‘œ Validation namespace λŠ” λ³‘ν•©λ©λ‹ˆλ‹€. ZipCodeValidators.ts 파일 μ—­μ‹œ λ§ˆμ°¬κ°€μ§€ μž…λ‹ˆλ‹€.

μœ„ μ½”λ“œλ₯Ό μ»΄νŒŒμΌν•œ JavaScript νŒŒμΌμ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/// <reference path="Validation.ts"/>
var Validation;
(function (Validation) {
  const lettersRegexp = /^[A-Za-z]+$/;
  class LettersOnlyValidator {
    isAcceptable(s) {
      return lettersRegexp.test(s);
    }
  }
  Validation.LettersOnlyValidator = LettersOnlyValidator;
})(Validation || (Validation = {}));
/// <reference path="Validation.ts"/>
var Validation;
(function (Validation) {
  const numberRegexp = /^[0-9]+$/;
  class ZipCodeValidator {
    isAcceptable(s) {
      return s.length === 5 && numberRegexp.test(s);
    }
  }
  Validation.ZipCodeValidator = ZipCodeValidator;
})(Validation || (Validation = {}));

Validation κ°μ²΄μ•ˆμ— λ³‘ν•©λœ class 듀이 μ§€μ •λ˜λŠ” 것을 λ³Ό 수 μžˆμŠ΅λ‹ˆλ‹€.

πŸ“” 참고자료

What is namespace in Typescript
How To Use Namespaces in TypeScript
TypeScript Namespaces
What are triple-slash Directives in TypeScript?

이 κΈ°μ‚¬λŠ” μ €μž‘κΆŒμžμ˜ CC BY 4.0 λΌμ΄μ„ΌμŠ€λ₯Ό λ”°λ¦…λ‹ˆλ‹€.

Typescript Module Resolution

νƒ€μž… μ„ μ–Έ 파일 (Type Definition File)