์ฒซ ๋ฒ์งธ ๋จ๊ณ (First steps)
๋ค์์คํ์ด์ค ์ ์ฉํ๊ธฐ (Namespacing)
ํ์ผ ๊ฐ ๋ถํ (Splitting Across Files)
๋ค๋ฅธ JavaScript ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ์์ ํ๊ธฐ (Working with Other JavaScript Libraries)
์ด ๊ฒ์๋ฌผ์์๋ TypeScript์์ ๋ค์์คํ์ด์ค๋ฅผ ์ฌ์ฉํ์ฌ ์ฝ๋๋ฅผ ๊ตฌ์ฑํ๋ ๋ค์ํ ๋ฐฉ๋ฒ์ ๊ฐ๋ตํ๊ฒ ์ค๋ช ํฉ๋๋ค.
์ด ํ์ด์ง ์ ์ฒด์์ ์์ ๋ก ์ฌ์ฉํ ํ๋ก๊ทธ๋จ์ ์์ํ๊ฒ ์ต๋๋ค. ์น ํ์ด์ง์ ์์์ ๋ํ ์ฌ์ฉ์ ์ ๋ ฅ์ ํ์ธํ๊ฑฐ๋ ์ธ๋ถ๋ก๋ถํฐ ๋ฐ์ ๋ฐ์ดํฐ ํ์ผ์ ํ์์ ํ์ธํ๊ธฐ ์ํด ๊ฐ๋จํ ๋ฌธ์์ด ๊ฒ์ฌ๊ธฐ ์ธํธ๋ฅผ ์์ฑํ์ต๋๋ค.
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 }'.`);
}
}
๋ ๋ง์ ๊ฒ์ฌ๊ธฐ๋ฅผ ์ถ๊ฐํ๊ฒ ๋๋ฉด, ํ์ ์ ์ถ์ ํ๊ณ ๋ค๋ฅธ ๊ฐ์ฒด์ ์ด๋ฆ ์ถฉ๋์ ๋ฐฉ์งํ๊ธฐ ์ํด ์ผ์ข ์ ๊ตฌ์กฐ ์ฒด๊ณ๊ฐ ํ์ํฉ๋๋ค. ์ ์ญ ๋ค์์คํ์ด์ค์ ๋ค๋ฅธ ์ด๋ฆ์ ๋ง์ด ๋ฃ๋ ๋์ , ๊ฐ์ฒด๋ค์ ํ๋์ ๋ค์์คํ์ด์ค๋ก ๊ฐ์ธ๊ฒ ์ต๋๋ค.
์ด ์์์๋ ๋ชจ๋ ๊ฒ์ฌ๊ธฐ ๊ด๋ จ ๊ฐ์ฒด๋ฅผ Validation
์ด๋ผ๋ ํ๋์ ๋ค์์คํ์ด์ค๋ก ์ฎ๊ธฐ๊ฒ ์ต๋๋ค.
์ฌ๊ธฐ์ ์ธํฐํ์ด์ค ๋ฐ ํด๋์ค๊ฐ ๋ค์์คํ์ด์ค ์ธ๋ถ์์๋ ์ ๊ทผ ๊ฐ๋ฅํ๋๋ก ์ ์ธ๋ถ์ export
๋ฅผ ๋ถ์
๋๋ค.
๋ฐ๋ฉด, ๋ณ์ letterRegexp
์ numberRegexp
๋ ๊ตฌํ ์ธ๋ถ ์ฌํญ์ด๋ฏ๋ก ์ธ๋ถ๋ก ๋ด๋ณด๋ด์ง ์์ ๋ค์์คํ์ด์ค ์ธ๋ถ ์ฝ๋์์ ์ ๊ทผํ ์ ์์ต๋๋ค.
ํ์ผ ํ๋จ์ ํ
์คํธ ์ฝ๋์์, ๋ค์์คํ์ด์ค ์ธ๋ถ์์ ์ฌ์ฉ๋ ๋ ํ์
์ ์ด๋ฆ์ ๊ฒ์ฆํด์ผ ํฉ๋๋ค (์: Validation.LetterOnlyValidator
).
namespace Validation {
export interface StringValidator {
isAcceptable(s: string): boolean;
}
const lettersRegexp = /^[A-Za-z]+$/;
const 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);
}
}
}
// ์๋ํด ๋ณผ ์ํ
let strings = ["Hello", "98052", "101"];
// ์ฌ์ฉํ ๊ฒ์ฌ๊ธฐ
let validators: { [s: string]: Validation.StringValidator; } = {};
validators["ZIP code"] = new Validation.ZipCodeValidator();
validators["Letters only"] = new Validation.LettersOnlyValidator();
// ๊ฐ ๋ฌธ์์ด์ด ๊ฐ ๊ฒ์ฌ๊ธฐ๋ฅผ ํต๊ณผํ๋์ง ํ์
for (let s of strings) {
for (let name in validators) {
console.log(`"${ s }" - ${ validators[name].isAcceptable(s) ? "matches" : "does not match" } ${ name }`);
}
}
์ ํ๋ฆฌ์ผ์ด์ ๊ท๋ชจ๊ฐ ์ปค์ง๋ฉด, ์ฝ๋๋ฅผ ์ฌ๋ฌ ํ์ผ๋ก ๋ถํ ํด์ผ ์ ์ง ๋ณด์๊ฐ ์ฉ์ดํฉ๋๋ค.
์ฌ๊ธฐ์ Validation
๋ค์์คํ์ด์ค๋ฅผ ์ฌ๋ฌ ํ์ผ๋ก ๋ถํ ํฉ๋๋ค.
ํ์ผ์ด ๋ถ๋ฆฌ๋์ด ์์ด๋ ๊ฐ์ ๋ค์์คํ์ด์ค์ ๊ธฐ์ฌํ ์ ์๊ณ ๋ง์น ํ ๊ณณ์์ ์ ์๋ ๊ฒ์ฒ๋ผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
ํ์ผ ๊ฐ ์์กด์ฑ์ด ์กด์ฌํ๋ฏ๋ก, ์ฐธ์กฐ ํ๊ทธ๋ฅผ ์ถ๊ฐํ์ฌ ์ปดํ์ผ๋ฌ์๊ฒ ํ์ผ ๊ฐ์ ๊ด๊ณ๋ฅผ ์๋ฆฝ๋๋ค.
๊ทธ ์ธ์ ํ
์คํธ ์ฝ๋๋ ๋ณ๊ฒฝ๋์ง ์์์ต๋๋ค.
namespace Validation{
export interface StringValidator{
isAcceptable(s: string): boolean;
}
}
/// <reference path="Validation.ts" />
namespace Validation {
const lettersRegexp = /^[A-Za-z]+$/;
export class LettersOnlyValidator implements StringValidator {
isAcceptable(s: string) {
return lettersRegexp.test(s);
}
}
}
/// <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);
}
}
}
/// <reference path="Validation.ts" />
/// <reference path="LettersOnlyValidator.ts" />
/// <reference path="ZipCodeValidator.ts" />
// Some samples to try
let strings = ["Hello", "98052", "101"];
// Validators to use
let validators: { [s: string]: Validation.StringValidator; } = {};
validators["ZIP code"] = new Validation.ZipCodeValidator();
validators["Letters only"] = new Validation.LettersOnlyValidator();
// Show whether each string passed each validator
for (let s of strings) {
for (let name in validators) {
console.log(`"${ s }" - ${ validators[name].isAcceptable(s) ? "matches" : "does not match" } ${ name }`);
}
}
ํ์ผ์ด ์ฌ๋ฌ ๊ฐ ์์ผ๋ฉด ์ปดํ์ผ๋ ์ฝ๋๊ฐ ๋ชจ๋ ๋ก๋๋๋์ง ํ์ธํด์ผ ํฉ๋๋ค. ์ด๋ฅผ ์ํํ๋ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์์ต๋๋ค.
๋จผ์ , ๋ชจ๋ ์
๋ ฅ ํ์ผ์ ํ๋์ JavaScript ์ถ๋ ฅ ํ์ผ๋ก ์ปดํ์ผํ๊ธฐ ์ํด --outFile
ํ๋๊ทธ๋ฅผ ์ฌ์ฉํ์ฌ ์ฐ๊ฒฐ ์ถ๋ ฅ(concatenated output)์ ์ฌ์ฉํ ์ ์์ต๋๋ค:
tsc --outFile sample.js Test.ts
์ปดํ์ผ๋ฌ๋ ํ์ผ์ ์๋ ์ฐธ์กฐ ํ๊ทธ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ถ๋ ฅ ํ์ผ์ ์๋์ผ๋ก ์ ๋ ฌํฉ๋๋ค. ๊ฐ ํ์ผ์ ๊ฐ๋ณ์ ์ผ๋ก ์ง์ ํ ์๋ ์์ต๋๋ค:
tsc --outFile sample.js Validation.ts LettersOnlyValidator.ts ZipCodeValidator.ts Test.ts
๋๋ ํ์ผ๋ณ ์ปดํ์ผ (๊ธฐ๋ณธ๊ฐ)์ ์ฌ์ฉํ์ฌ ๊ฐ ์
๋ ฅ ํ์ผ์ ํ๋์ JavaScript ํ์ผ๋ก ์์ฑํ ์ ์์ต๋๋ค.
์ฌ๋ฌ JS ํ์ผ์ด ์์ฑ๋๋ ๊ฒฝ์ฐ, ์น ํ์ด์ง์์ ์์ฑ๋ ๊ฐ๋ณ ํ์ผ์ ์ ์ ํ ์์๋ก ๋ก๋ํ๊ธฐ ์ํด <script>
ํ๊ทธ๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค. ์๋ฅผ ๋ค์ด:
<script src="Validation.js" type="text/javascript" />
<script src="LettersOnlyValidator.js" type="text/javascript" />
<script src="ZipCodeValidator.js" type="text/javascript" />
<script src="Test.js" type="text/javascript" />
๋ค์์คํ์ด์ค ์์
์ ๋จ์ํํ ์ ์๋ ๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ๋๋ ๊ฐ์ฒด์ ์ด๋ฆ์ ๋ ์งง๊ฒ ๋ง๋ค๊ธฐ ์ํด import q = x.y.z
๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์
๋๋ค.
๋ชจ๋์ ๋ก๋ํ๋ ๋ฐ ์ฌ์ฉ๋๋ import x = require ("name")
๊ตฌ๋ฌธ๊ณผ ํผ๋ํ์ง ์๊ธฐ ์ํด, ์ด ๊ตฌ๋ฌธ์ ๋จ์ํ ํน์ ์ฌ๋ฒ์ ๋ณ์นญ์ ์์ฑํฉ๋๋ค.
์ด๋ฌํ ์ข
๋ฅ์ ๊ฐ์ ธ์ค๊ธฐ(์ผ๋ฐ์ ์ผ๋ก ๋ณ์นญ์ด๋ผ๊ณ ํจ)๋ ๋ชจ๋ ๊ฐ์ ธ์ค๊ธฐ์์ ์์ฑ๋ ๊ฐ์ฒด๋ฅผ ํฌํจํ์ฌ ๋ชจ๋ ์ข
๋ฅ์ ์๋ณ์์ ๋ํด ์ฌ์ฉํ ์ ์์ต๋๋ค.
namespace Shapes {
export namespace Polygons {
export class Triangle { }
export class Square { }
}
}
import polygons = Shapes.Polygons;
let sq = new polygons.Square(); // 'new Shapes.Polygons.Square()'์ ๋์ผ
require
ํค์๋๋ฅผ ์ฌ์ฉํ์ง ์๋๋ค๋ ๊ฒ์ ๋ช
์ฌํ์ธ์; ๋์ ๊ฐ์ ธ์ค๋ ์ฌ๋ฒ์ ์ ํด์ง ์ด๋ฆ์ผ๋ก ์ง์ ํ ๋นํฉ๋๋ค.
var
๋ฅผ ์ฌ์ฉํ๋ ๊ฒ๊ณผ ๋น์ทํ์ง๋ง, ๊ฐ์ ธ์จ ์ฌ๋ฒ์ ํ์
๋ฐ ๋ค์์คํ์ด์ค ์๋ฏธ์ ๋ํด์๋ ๋์ํฉ๋๋ค.
ํนํ, ๊ฐ์ ๊ฒฝ์ฐ import
๋ ์๋ ์ฌ๋ฒ์ ๋ณ๊ฐ์ ์ฐธ์กฐ์ด๋ฏ๋ก ๋ณ์นญ var
์ ๋ํ ๋ณ๊ฒฝ ๋ด์ฉ์ ์๋ ๋ณ์์ ๋ฐ์๋์ง ์์ต๋๋ค.
TypeScript๋ก ์์ฑ๋์ง ์์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ํํ๋ฅผ ์ค๋ช ํ๋ ค๋ฉด, ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ธ๋ถ์ ์ ๊ณตํ๋ API๋ฅผ ์ ์ธํด์ผ ํฉ๋๋ค. ๋๋ถ๋ถ์ JavaScript ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ์์์ ์ต์์ ๊ฐ์ฒด๋ง ๋ ธ์ถํ๋ฏ๋ก ๋ค์์คํ์ด์ค๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
๊ตฌํ์ ์ ์ํ์ง ์์ ์ ์ธ์ "ambient"๋ผ๊ณ ๋ถ๋ฆ
๋๋ค.
์ผ๋ฐ์ ์ผ๋ก ์ด๊ฒ์ .d.ts
ํ์ผ์ ์ ์๋์ด ์์ต๋๋ค.
C/C++์ ์ต์ํ๋ค๋ฉด ์ด๋ฅผ .h
ํ์ผ๋ก ์๊ฐํ ์ ์์ต๋๋ค.
๋ช ๊ฐ์ง ์๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
๋๋ฆฌ ์ฌ์ฉ๋๋ D3 ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ d3
์ด๋ผ๋ ์ ์ญ ๊ฐ์ฒด์์ ๊ธฐ๋ฅ์ ์ ์ํฉ๋๋ค.
์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ <script>
ํ๊ทธ๋ฅผ ํตํด ๋ก๋๋๋ฏ๋ก(๋ชจ๋ ๋ก๋ ๋์ ) ํํ๋ฅผ ์ ์ํ๊ธฐ ์ํด ์ ์ธํ ๋ ๋ค์์คํ์ด์ค๋ฅผ ์ฌ์ฉํฉ๋๋ค.
TypeScript ์ปดํ์ผ๋ฌ๋ ์ด ํํ๋ฅผ ๋ณด๊ธฐ ์ํด, ambient ๋ค์์คํ์ด์ค ์ ์ธ์ ์ฌ์ฉํฉ๋๋ค.
์๋ฅผ ๋ค์ด ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ ์ ์์ต๋๋ค:
declare namespace D3 {
export interface Selectors {
select: {
(selector: string): Selection;
(element: EventTarget): Selection;
};
}
export interface Event {
x: number;
y: number;
}
export interface Base extends Selectors {
event: Event;
}
}
declare var d3: D3.Base;