π» 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.jsBrowser
script
νκ·Έμ μμ‘΄μ±μ κ°λ μμλλ‘ μ»΄νμΌλ λͺ¨λλ€μ λͺ μν΄ μ£Όκ±°λ,outFile
μ΅μ μ μ§μ ν΄ νλλ‘ ν©μ³μ§ νμΌμ λͺ μν΄ μ£Όμ΄ μ€νμν¬ μ μμ΅λλ€.
π Triple-Slash Directives μ’ λ₯
- <reference path=ββ¦β/ >
ν΄λΉ μ§μμ΄λ νμΌ κ°μ μμ‘΄μ± μ μΈμΌλ‘ μ¬μ©λ©λλ€. μ΄ μ§μμ΄κ° νμΌμ μμμ μ‘΄μ¬νλ©΄ μ»΄νμΌλ¬λ μΆκ° νμΌμ μ»΄νμΌ κ³Όμ μ ν¬ν¨μν΅λλ€.
λν μμμ μ΄ν΄λ³΄μλ―μ΄,--outFile
μ΅μ μ μ¬μ©ν λ λͺ¨λμ μ λ ¬ μμλ₯Ό μ§μ νλ μν λ ν©λλ€.- <reference types=ββ¦β/ >
ν¨ν€μ§μ μμ‘΄μ±μ μ μΈνλ μ§μμ΄μ λλ€.
Type declaration
νμΌμ <reference types=βnodeβ/ > λ₯Ό μ μΈνλ κ²μ@types/node/index.d.ts
μ μ μΈλ νμ μ μ¬μ©νλ€κ³ μ리λ κ²μ΄κ³ , μ΄ ν¨ν€μ§λ μ μΈ νμΌκ³Ό ν¨κ» μ»΄νμΌμ ν¬ν¨λ©λλ€.- <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?