ECMAScript 2015λΆν° JavaScriptμλ λͺ¨λ κ°λ μ΄ μμ΅λλ€. TypeScriptλ μ΄ κ°λ μ 곡μ ν©λλ€.
λͺ¨λμ μ μ μ€μ½νκ° μλ μ체 μ€μ½ν λ΄μμ μ€νλ©λλ€; μ¦ λͺ¨λ λ΄μμ μ μΈλ λ³μ, ν¨μ, ν΄λμ€ λ±μ export
μμ μ€ νλλ₯Ό μ¬μ©νμ¬ λͺ
μμ μΌλ‘ export νμ§ μλ ν λͺ¨λ μΈλΆμμ 보μ΄μ§ μμ΅λλ€.
λ°λλ‘ λ€λ₯Έ λͺ¨λμμ export ν λ³μ, ν¨μ, ν΄λμ€, μΈν°νμ΄μ€ λ±μ μ¬μ©νκΈ° μν΄μλ import
μμ μ€ νλλ₯Ό μ¬μ©νμ¬ import ν΄μΌ ν©λλ€.
λͺ¨λμ μ μΈνμ λλ€; λͺ¨λ κ°μ κ΄κ³λ νμΌ μμ€μ imports λ° exports κ΄μ μμ μ§μ λ©λλ€.
λͺ¨λμ λͺ¨λ λ‘λλ₯Ό μ¬μ©νμ¬ λ€λ₯Έ λͺ¨λμ import ν©λλ€. λ°νμ μ λͺ¨λ λ‘λλ λͺ¨λμ μ€ννκΈ° μ μ λͺ¨λμ λͺ¨λ μμ‘΄μ±μ μ°Ύκ³ μ€νν΄μΌ ν©λλ€. JavaScriptμμ μ¬μ©νλ μ λͺ ν λͺ¨λ λ‘λλ‘λ CommonJS λͺ¨λ μ© Node.jsμ λ‘λμ μΉ μ ν리μΌμ΄μ μ AMD λͺ¨λ μ© RequireJS λ‘λκ° μμ΅λλ€.
ECMAScript 2015μ λ§μ°¬κ°μ§λ‘ TypeScriptλ μ΅μμ μμ€μ import
νΉμ export
κ° ν¬ν¨λ λͺ¨λ νμΌμ λͺ¨λλ‘ κ°μ£Όν©λλ€.
λ°λλ‘ μ΅μμ μμ€μ import
νΉμ export
μ μΈμ΄ μλ νμΌμ μ μ μ€μ½νμμ μ¬μ©ν μ μλ μ€ν¬λ¦½νΈλ‘ μ²λ¦¬λ©λλ€(λͺ¨λμμλ λ§μ°¬κ°μ§).
export
ν€μλλ₯Ό μΆκ°νμ¬ λͺ¨λ μ μΈ (λ³μ, ν¨μ, ν΄λμ€, νμ
λ³μΉ, μΈν°νμ΄μ€)λ₯Ό export ν μ μμ΅λλ€.
export interface StringValidator {
isAcceptable(s: string): boolean;
}
import { StringValidator } from "./StringValidator";
export const numberRegexp = /^[0-9]+$/;
export class ZipCodeValidator implements StringValidator {
isAcceptable(s: string) {
return s.length === 5 && numberRegexp.test(s);
}
}
Export λ¬Έμ μ¬μ©μλ₯Ό μν΄ export ν μ΄λ¦μ λ°κΏμΌ ν λ νΈλ¦¬ν©λλ€. μμ μμ λ λ€μκ³Ό κ°μ΄ μμ±ν μ μμ΅λλ€:
class ZipCodeValidator implements StringValidator {
isAcceptable(s: string) {
return s.length === 5 && numberRegexp.test(s);
}
}
export { ZipCodeValidator };
export { ZipCodeValidator as mainValidator };
μ’ μ’ λͺ¨λμ λ€λ₯Έ λͺ¨λμ νμ₯νκ³ μΌλΆ κΈ°λ₯μ λΆλΆμ μΌλ‘ λ ΈμΆν©λλ€. Re-export νκΈ°λ μ§μμ μΌλ‘ import νκ±°λ, μ§μ λ³μλ₯Ό λμ νμ§ μμ΅λλ€.
export class ParseIntBasedZipCodeValidator {
isAcceptable(s: string) {
return s.length === 5 && parseInt(s).toString() === s;
}
}
// κΈ°μ‘΄ validatorμ μ΄λ¦μ λ³κ²½ ν export
export {ZipCodeValidator as RegExpBasedZipCodeValidator} from "./ZipCodeValidator";
μ νμ μΌλ‘, νλμ λͺ¨λμ νλ νΉμ μ¬λ¬ κ°μ λͺ¨λμ κ°μ μ μκ³ , export * from "module"
ꡬ문μ μ¬μ©ν΄ export νλ κ²μ λͺ¨λ κ²°ν©ν μ μμ΅λλ€.
export * from "./StringValidator"; // 'StringValidator' μΈν°νμ΄μ€λ₯Ό λ΄λ³΄λ
export * from "./ZipCodeValidator"; // 'ZipCodeValidator' μ const 'numberRegexp' ν΄λμ€λ₯Ό λ΄λ³΄λ
export * from "./ParseIntBasedZipCodeValidator"; // 'ParseIntBasedZipCodeValidator' ν΄λμ€λ₯Ό λ΄λ³΄λ
// 'ZipCodeValidator.ts' λͺ¨λ μ μλ
// 'ZipCodeValidator' ν΄λμ€λ₯Ό
// 'RegExpBasedZipCodeValidator' λΌλ λ³μΉμΌλ‘ λ€μ λ΄λ³΄λ
importλ λͺ¨λμμ export λ§νΌ μ½μ΅λλ€.
export ν μ μΈμ μλμ import
μμ μ€ νλλ₯Ό μ¬μ©νμ¬ import ν©λλ€:
import { ZipCodeValidator } from "./ZipCodeValidator";
let myValidator = new ZipCodeValidator();
μ΄λ¦μ μμ ν΄μ import ν μ μμ΅λλ€.
import { ZipCodeValidator as ZCV } from "./ZipCodeValidator";
let myValidator = new ZCV();
μ 체 λͺ¨λμ λ¨μΌ λ³μλ‘ import ν΄μ, λͺ¨λ exports μ κ·Όμ μ¬μ©νκΈ° (Import the entire module into a single variable, and use it to access the module exports)
import * as validator from "./ZipCodeValidator";
let myValidator = new validator.ZipCodeValidator();
κΆμ₯λμ§λ μμ§λ§, μΌλΆ λͺ¨λμ λ€λ₯Έ λͺ¨λμμ μ¬μ©ν μ μλλ‘ μΌλΆ μ μ μνλ‘ μ€μ ν©λλ€. μ΄λ¬ν λͺ¨λμ μ΄λ€ exportsλ μκ±°λ, μ¬μ©μκ° exportsμ κ΄μ¬μ΄ μμ΅λλ€. μ΄λ¬ν λͺ¨λμ import νκΈ° μν΄, λ€μμ²λΌ μ¬μ©νμΈμ:
import "./my-module.js"
TypeScript 3.8 μ΄μ μλ import
λ₯Ό μ¬μ©νμ¬ νμ
μ import ν μ μμμ΅λλ€.
TypeScript 3.8μμλ import
λ¬Έ νΉμ import type
μ μ¬μ©νμ¬ νμ
μ import ν μ μμ΅λλ€.
// λμΌν importλ₯Ό μ¬μ¬μ©νκΈ°
import {APIResponseType} from "./api";
// λͺ
μμ μΌλ‘ import typeμ μ¬μ©νκΈ°
import type {APIResponseType} from "./api";
import type
μ νμ JavaScriptμμ μ κ±°λλ©°, λ°λ²¨ κ°μ λꡬλ isolatedModules
μ»΄νμΌλ¬ νλκ·Έλ₯Ό ν΅ν΄ μ½λμ λν΄ λ λμ κ°μ μ ν μ μμ΅λλ€.
3.8 λ¦΄λ¦¬μ¦ μ 보μμ λ λ§μ μ 보λ₯Ό μ½μ μ μμ΅λλ€.
κ° λͺ¨λμ μ νμ μΌλ‘ default
exportλ₯Ό export ν μ μμ΅λλ€.
default exportλ default
ν€μλλ‘ νμλ©λλ€; λͺ¨λλΉ νλμ default
exportλ§ κ°λ₯ν©λλ€.
default
exportλ λ€λ₯Έ import μμμ μ¬μ©νμ¬ import ν©λλ€.
default
exportsλ μ λ§ νΈλ¦¬ν©λλ€.
μλ₯Ό λ€μ΄ jQueryμ κ°μ λΌμ΄λΈλ¬λ¦¬λ jQuery
νΉμ $
μ κ°μ default exportλ₯Ό κ°μ§ μ μμΌλ©°, $
λ jQuery
μ κ°μ μ΄λ¦μΌλ‘ importν μ μμ΅λλ€.
declare let $: JQuery;
export default $;
import $ from "jquery";
$("button.continue").html( "Next Step..." );
ν΄λμ€ λ° ν¨μ μ μΈμ default exportsλ‘ μ§μ μμ±λ μ μμ΅λλ€. default export ν΄λμ€ λ° ν¨μ μ μΈ μ΄λ¦μ μ νμ¬ν μ λλ€.
export default class ZipCodeValidator {
static numberRegexp = /^[0-9]+$/;
isAcceptable(s: string) {
return s.length === 5 && ZipCodeValidator.numberRegexp.test(s);
}
}
import validator from "./ZipCodeValidator";
let myValidator = new validator();
νΉμ
const numberRegexp = /^[0-9]+$/;
export default function (s: string) {
return s.length === 5 && numberRegexp.test(s);
}
import validate from "./StaticZipCodeValidator";
let strings = ["Hello", "98052", "101"];
// validate ν¨μ μ¬μ©νκΈ°
strings.forEach(s => {
console.log(`"${s}" ${validate(s) ? "matches" : "does not match"}`);
});
default
exportsλ κ°λ κ°λ₯ν©λλ€:
export default "123";
import num from "./OneTwoThree";
console.log(num); // "123"
TypeScript 3.8μμλ λ€μ μ΄λ¦μ΄ λ€λ₯Έ λͺ¨λλ‘ re-export λ λ λ¨μΆμ΄μ²λΌ export * as ns
λ₯Ό μ¬μ©ν μ μμ΅λλ€:
export * as utilities from "./utilities";
λͺ¨λμμ λͺ¨λ μμ‘΄μ±μ κ°μ Έμ exportν νλλ‘ λ§λ€λ©΄, λ€μκ³Ό κ°μ΄ importν μ μμ΅λλ€:
import { utilities } from "./index";
CommonJSμ AMD λ λ€ μΌλ°μ μΌλ‘ λͺ¨λμ λͺ¨λ exportsλ₯Ό ν¬ν¨νλ exports
κ°μ²΄μ κ°λ
μ κ°μ§κ³ μμ΅λλ€.
λν exports
κ°μ²΄λ₯Ό μ¬μ©μ μ μ λ¨μΌ κ°μ²΄λ‘ λ체νλ κΈ°λ₯λ μ§μν©λλ€.
default exportsλ μ΄ λμμμ λ체 μν μ ν©λλ€; νμ§λ§ λμ νΈνλμ§λ μμ΅λλ€.
TypeScriptλ κΈ°μ‘΄μ CommonJSμ AMD μν¬νλ‘μ°λ₯Ό λͺ¨λΈλ§ νκΈ° μν΄ export =
λ₯Ό μ§μν©λλ€.
export =
ꡬ문μ λͺ¨λμμ exportλλ λ¨μΌ κ°μ²΄λ₯Ό μ§μ ν©λλ€.
ν΄λμ€, μΈν°νμ΄μ€, λ€μμ€νμ΄μ€, ν¨μ νΉμ μ΄κ±°νμ΄ λ μ μμ΅λλ€.
export =
λ₯Ό μ¬μ©νμ¬ λͺ¨λμ exportν λ, TypeScriptμ νΉμ ν import module = require("module")
λ₯Ό μ¬μ©νμ¬ λͺ¨λμ κ°μ ΈμμΌ ν©λλ€.
let numberRegexp = /^[0-9]+$/;
class ZipCodeValidator {
isAcceptable(s: string) {
return s.length === 5 && numberRegexp.test(s);
}
}
export = ZipCodeValidator;
import zip = require("./ZipCodeValidator");
// μνμ© μν
let strings = ["Hello", "98052", "101"];
// μ¬μ©ν Validators
let validator = new zip();
// κ° λ¬Έμμ΄μ΄ κ° validatorλ₯Ό ν΅κ³Όνλμ§ λ³΄μ¬μ€λλ€
strings.forEach(s => {
console.log(`"${ s }" - ${ validator.isAcceptable(s) ? "matches" : "does not match" }`);
});
μ»΄νμΌ μ€μλ μ§μ λ λͺ¨λ λμμ λ°λΌ μ»΄νμΌλ¬λ Node.js (CommonJS), require.js (AMD), UMD, SystemJS, λλ ECMAScript 2015 native modules (ES6) λͺ¨λ-λ‘λ© μμ€ν
μ μ ν©ν μ½λλ₯Ό μμ±ν©λλ€.
μμ±λ μ½λμ define
, require
κ·Έλ¦¬κ³ register
νΈμΆ κΈ°λ₯μ λν μμΈν μ 보λ κ° λͺ¨λ λ‘λμ λ¬Έμλ₯Ό νμΈνμΈμ.
μ΄ κ°λ¨ν μμ λ import λ° export νκΈ° μ€μ μ¬μ©λ μ΄λ¦μ΄ λͺ¨λ λ‘λ© μ½λλ‘ λ³νλλ λ°©λ²μ 보μ¬μ€λλ€.
import m = require("mod");
export let t = m.something + 1;
define(["require", "exports", "./mod"], function (require, exports, mod_1) {
exports.t = mod_1.something + 1;
});
var mod_1 = require("./mod");
exports.t = mod_1.something + 1;
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports); if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "./mod"], factory);
}
})(function (require, exports) {
var mod_1 = require("./mod");
exports.t = mod_1.something + 1;
});
System.register(["./mod"], function(exports_1) {
var mod_1;
var t;
return {
setters:[
function (mod_1_1) {
mod_1 = mod_1_1;
}],
execute: function() {
exports_1("t", t = mod_1.something + 1);
}
}
});
import { something } from "./mod";
export var t = something + 1;
μλμμλ κ° λͺ¨λμμ λ¨μΌ μ΄λ¦μΌλ‘ export νκΈ° μν΄ μ΄μ μμ μμ μ¬μ©ν Validator ꡬνμ ν΅ν©ν©λλ€.
μ»΄νμΌ νλ €λ©΄, λͺ
λ Ή μ€μμ λͺ¨λ λμμ μ§μ ν΄μΌ ν©λλ€. Node.jsμ κ²½μ°, --module commonjs
λ₯Ό μ¬μ©νμΈμ;
require.jsμ κ²½μ° --module amd
λ₯Ό μ¬μ©νμΈμ. μλ₯Ό λ€λ©΄:
tsc --module commonjs Test.ts
μ»΄νμΌμ΄ λλ©΄, κ° λͺ¨λμ λ³λμ .js
νμΌμ΄ λ©λλ€.
μ°Έμ‘° νκ·Έμ λ§μ°¬κ°μ§λ‘, μ»΄νμΌλ¬λ import
λ¬Έμ λ°λΌ μμ‘΄μ μΈ νμΌλ€μ μ»΄νμΌ ν©λλ€.
export interface StringValidator {
isAcceptable(s: string): boolean;
}
import { StringValidator } from "./Validation";
const lettersRegexp = /^[A-Za-z]+$/;
export class LettersOnlyValidator implements StringValidator {
isAcceptable(s: string) {
return lettersRegexp.test(s);
}
}
import { StringValidator } from "./Validation";
const numberRegexp = /^[0-9]+$/;
export class ZipCodeValidator implements StringValidator {
isAcceptable(s: string) {
return s.length === 5 && numberRegexp.test(s);
}
}
import { StringValidator } from "./Validation";
import { ZipCodeValidator } from "./ZipCodeValidator";
import { LettersOnlyValidator } from "./LettersOnlyValidator";
// μνμ© μν
let strings = ["Hello", "98052", "101"];
// μ¬μ©ν validator
let validators: { [s: string]: StringValidator; } = {};
validators["ZIP code"] = new ZipCodeValidator();
validators["Letters only"] = new LettersOnlyValidator();
// κ° λ¬Έμμ΄μ΄ validatorλ₯Ό ν΅κ³Όνλμ§ λ³΄μ¬μ€
strings.forEach(s => {
for (let name in validators) {
console.log(`"${ s }" - ${ validators[name].isAcceptable(s) ? "matches" : "does not match" } ${ name }`);
}
});
μ νμ λͺ¨λ λ‘λ©κ³Ό κΈ°ν κ³ κΈ λ‘λ© μλλ¦¬μ€ (Optional Module Loading and Other Advanced Loading Scenarios)
μν©μ λ°λΌ νΉμ 쑰건μμλ§ λͺ¨λμ λ‘λνλλ‘ λ§λ€ μ μμ΅λλ€. TypeScriptμμλ μλμ μλ ν¨ν΄μ μ¬μ©νμ¬ μ΄ μλ리μ€μ λ€λ₯Έ κ³ κΈ λ‘λ© μλ리μ€λ₯Ό ꡬννμ¬ νμ μ μμ μ±μ μμ§ μκ³ λͺ¨λ λ‘λλ₯Ό μ§μ νΈμΆν μ μμ΅λλ€.
μ»΄νμΌλ¬λ λ
ΈμΆλ JavaScript μμμ κ° λͺ¨λμ μ¬μ© μ¬λΆλ₯Ό κ°μ§ν©λλ€.
λͺ¨λ μλ³μκ° ννμμ΄ μλ νμ
νμλ‘λ§ μ¬μ©λλ€λ©΄ κ·Έ λͺ¨λμ λν require
νΈμΆμ λ°μνμ§ μμ΅λλ€.
μ¬μ©νμ§ μλ μ°Έμ‘°λ₯Ό μ κ±°νλ©΄ μ±λ₯μ μ΅μ νν μ μμΌλ©°, ν΄λΉ λͺ¨λμ μ νμ μΌλ‘ λ‘λ© ν μ μμ΅λλ€.
μ΄ ν¨ν΄μ ν΅μ¬ μμ΄λμ΄λ import id = require("...")
λ¬Έμ ν΅ν΄ λͺ¨λλ‘ λ
ΈμΆλ νμ
μ μ κ·Όμ΄ κ°λ₯νλ€λ κ²μ
λλ€.
μλ if
λΈλ‘μ 보μ΄λ κ²μ²λΌ, λͺ¨λ λ‘λλ (require
μ ν΅ν΄) λμ μΌλ‘ νΈμΆλ©λλ€.
μ΄ κΈ°λ₯μ μ°Έμ‘°-μ κ±° μ΅μ νλ₯Ό νμ©νλ―λ‘ νμν λλ§ λͺ¨λμ λ‘λν μ μμ΅λλ€.
ν΄λΉ ν¨ν΄μ΄ λμνλ €λ©΄ import
λ₯Ό ν΅ν΄ μ μλ μ¬λ²μ μ€μ§ νμ
μμΉ(μ¦, JavaScriptλ‘ λ°©μΆλλ μμΉμμλ μ¬μ© μ ν¨)μμλ§ μ¬μ©λλ κ²μ΄ μ€μν©λλ€.
νμ
μμ μ±μ μ μ§νκΈ° μν΄, typeof
ν€μλλ₯Ό μ¬μ©ν μ μμ΅λλ€.
typeof
ν€μλλ νμ
μμΉμμ μ¬μ©λ λλ κ°μ νμ
, μ΄ κ²½μ°μλ λͺ¨λμ νμ
μ μμ±ν©λλ€.
declare function require(moduleName: string): any;
import { ZipCodeValidator as Zip } from "./ZipCodeValidator";
if (needZipValidation) {
let ZipCodeValidator: typeof Zip = require("./ZipCodeValidator");
let validator = new ZipCodeValidator();
if (validator.isAcceptable("...")) { /* ... */ }
}
declare function require(moduleNames: string[], onLoad: (...args: any[]) => void): void;
import * as Zip from "./ZipCodeValidator";
if (needZipValidation) {
require(["./ZipCodeValidator"], (ZipCodeValidator: typeof Zip) => {
let validator = new ZipCodeValidator.ZipCodeValidator();
if (validator.isAcceptable("...")) { /* ... */ }
});
}
declare const System: any;
import { ZipCodeValidator as Zip } from "./ZipCodeValidator";
if (needZipValidation) {
System.import("./ZipCodeValidator").then((ZipCodeValidator: typeof Zip) => {
var x = new ZipCodeValidator();
if (x.isAcceptable("...")) { /* ... */ }
});
}
TypeScriptλ‘ μμ±λμ§ μμ λΌμ΄λΈλ¬λ¦¬μ ννλ₯Ό μ€λͺ νλ €λ©΄, λΌμ΄λΈλ¬λ¦¬λ₯Ό λ ΈμΆνλ APIλ₯Ό μ μΈν΄μΌ ν©λλ€.
μ°λ¦¬λ ꡬνμ μ μνμ§ μμ μ μΈμ "ambient"λΌκ³ λΆλ¦
λλ€.
μ΄ μ μΈλ€μ μΌλ°μ μΌλ‘ .d.ts
νμΌμ μ μλμ΄ μμ΅λλ€.
C/C++μ μ΅μνλ€λ©΄, .h
νμΌμ΄λΌκ³ μκ°ν μ μμ΅λλ€.
λͺ κ°μ§ μμ λ₯Ό μ΄ν΄λ³΄κ² μ΅λλ€.
Node.jsμμλ λλΆλΆμ μμ
μ νλ μ΄μμ λͺ¨λμ λ‘λνμ¬ μνν©λλ€.
μ΅μμ-λ 벨μ λ΄λ³΄λ΄κΈ° μ μΈμΌλ‘ κ° λͺ¨λμ .d.ts
νμΌλ‘ μ μν μ μμ§λ§, λ ν° νλμ .d.ts
νμΌλ‘ λͺ¨λλ€μ μμ±νλ κ²μ΄ λ νΈλ¦¬ν©λλ€.
μ΄λ₯Ό μν΄, ambient λ€μμ€νμ΄μ€μ μ μ¬ν ꡬ쑰λ₯Ό μ¬μ©νμ§λ§, λμ€μ import ν μ μλ μΈμ©λ λͺ¨λ μ΄λ¦κ³Ό module
ν€μλλ₯Ό μ¬μ©ν©λλ€.
μλ₯Ό λ€λ©΄:
declare module "url" {
export interface Url {
protocol?: string;
hostname?: string;
pathname?: string;
}
export function parse(urlStr: string, parseQueryString?, slashesDenoteHost?): Url;
}
declare module "path" {
export function normalize(p: string): string;
export function join(...paths: any[]): string;
export var sep: string;
}
μ΄μ /// <reference>
node.d.ts
λ₯Ό μνν λ€μ, import url = require("url");
λλ import * as URL from "url"
μ μ¬μ©νμ¬ λͺ¨λμ λ‘λν μ μμ΅λλ€.
/// <reference path="node.d.ts"/>
import * as URL from "url";
let myUrl = URL.parse("http://www.typescriptlang.org");
μλ‘μ΄ λͺ¨λμ μ¬μ©νκΈ° μ μ μ μΈμ μμ±νμ§ μλ κ²½μ°, μκΈ° μ μΈ(shorthand declaration)μ μ¬μ©νμ¬ λΉ λ₯΄κ² μμν μ μμ΅λλ€.
declare module "hot-new-module";
μκΈ° λͺ¨λλ‘λΆν° λͺ¨λ importsλ any
νμ
μ κ°μ§λλ€.
import x, {y} from "hot-new-module";
x(y);
SystemJSλ AMDμ κ°μ λͺ¨λ λ‘λλ λΉ-JavaScirpt λ΄μ©μ import ν μ μμ΅λλ€. μ΄ λμ μΌλ°μ μΌλ‘ μ λμ¬ λλ μ λ―Έμ¬λ₯Ό μ¬μ©νμ¬ νΉμν λ‘λ© μλ―Έλ₯Ό νμν©λλ€. μ΄λ¬ν κ²½μ°λ₯Ό λ€λ£¨κΈ° μν΄ μμΌλμΉ΄λ λͺ¨λ μ μΈμ μ¬μ©ν μ μμ΅λλ€.
declare module "*!text" {
const content: string;
export default content;
}
// μΌλΆλ λ€λ₯Έ λ°©λ²μΌλ‘ μ¬μ©ν©λλ€.
declare module "json!*" {
const value: any;
export default value;
}
μ΄μ "*!text"
λ "json!*"
μ μΌμΉνλ κ²λ€μ import ν μ μμ΅λλ€.
import fileContent from "./xyz.txt!text";
import data from "json!http://example.com/data.json";
console.log(data, fileContent);
μΌλΆ λΌμ΄λΈλ¬λ¦¬λ λ§μ λͺ¨λ λ‘λμμ μ¬μ©λκ±°λ, λͺ¨λ λ‘λ© (μ μ λ³μ) μμ΄ μ¬μ©λλλ‘ μ€κ³λμμ΅λλ€. μ΄λ₯Ό UMD λͺ¨λμ΄λΌκ³ ν©λλ€. μ΄λ¬ν λΌμ΄λΈλ¬λ¦¬λ importλ μ μ λ³μλ₯Ό ν΅ν΄ μ κ·Όν μ μμ΅λλ€. μλ₯Ό λ€λ©΄:
export function isPrime(x: number): boolean;
export as namespace mathLib;
λΌμ΄λΈλ¬λ¦¬λ λͺ¨λ λ΄μμ importλ‘ μ¬μ©ν μ μμ΅λλ€:
import { isPrime } from "math-lib";
isPrime(2);
mathLib.isPrime(2); // μ€λ₯: λͺ¨λ λ΄λΆμμ μ μ μ μλ₯Ό μ¬μ©ν μ μμ΅λλ€.
μ μ λ³μλ‘λ μ¬μ©ν μ μμ§λ§, μ€ν¬λ¦½νΈ λ΄μμλ§ μ¬μ©ν μ μμ΅λλ€. (μ€ν¬λ¦½νΈλ importsλ exportsκ° μλ νμΌμ λλ€.)
mathLib.isPrime(2);
λͺ¨λμ μ¬μ©μκ° export λͺ¨λμ μ¬μ©ν λ κ°λ₯ν λ§μ°°μ΄ μ μ΄μΌ ν©λλ€. μ€μ²© μμ€μ κ³Όλνκ² μΆκ°νλ©΄ λ€λ£¨κΈ° νλ€μ΄μ§λ κ²½ν₯μ΄ μμΌλ―λ‘, μ΄λ»κ² ꡬ쑰λ₯Ό ꡬμ±ν μ§ μ μ€νκ² μκ°ν΄μΌ ν©λλ€.
λͺ¨λμμ λ€μμ€νμ΄μ€λ₯Ό export νλ κ²μ λ무 λ§μ μ€μ²© λ μ΄μ΄λ₯Ό μΆκ°νλ μμ λλ€. λ€μμ€νμ΄μ€λ λλλ‘ μ©λκ° μμ§λ§, λͺ¨λμ μ¬μ©ν λ μΆκ°μ μΈ λ 벨μ κ°μ μ°Έμ‘°λ₯Ό μΆκ°ν©λλ€. μ΄κ²μ μ¬μ©μμκ² κΈλ°© κ³ ν΅μ€λ¬μ΄ μ§μ μ΄ λ μ μκ³ , μΌλ°μ μΌλ‘ λΆνμν©λλ€.
export ν ν΄λμ€μ μ μ λ©μλμλ λΉμ·ν λ¬Έμ κ° μμ΅λλ€ - ν΄λμ€ μ체μ μ€μ²© λ μ΄μ΄κ° μΆκ°λ©λλ€. ννμ΄λ μλλ₯Ό λͺ ννκ² μ μ©ν λ°©μμΌλ‘ λμ΄μ§ μλ ν κ°λ¨νκ² ν¬νΌ ν¨μλ₯Ό export νλ κ²μ κ³ λ €νμΈμ.
λ¨μΌ class
λ function
μ export ν κ²½μ°, export default
λ₯Ό μ¬μ©νμΈμ (If you're only exporting a single class
or function
, use export default
)
"μ΅μμ-λ 벨μ κ°κΉμ΄ export"κ° λͺ¨λ μ¬μ©μμ λ§μ°°μ μ€μ¬μ£Όλ κ²μ²λΌ, default exportλ₯Ό λμ νλ κ²λ λ§μ°¬κ°μ§μ λλ€. λͺ¨λμ μ£Όμ λͺ©μ μ΄ ν κ°μ νΉμ exportλ₯Ό μ μ₯νλ κ²μ΄λΌλ©΄, default exportλ‘ export νλ κ²μ κ³ λ €νμΈμ. μ΄λ κ² νλ©΄ import νκΈ°μ μ€μ λ‘ importλ₯Ό μ¬μ©νκΈ°κ° λ μ¬μμ§λλ€. μλ₯Ό λ€λ©΄:
export default class SomeType {
constructor() { ... }
}
export default function getThing() { return "thing"; }
import t from "./MyClass";
import f from "./MyFunc";
let x = new t();
console.log(f());
μ΄κ²μ μ¬μ©μμκ² μ΅μ μ
λλ€. νμ
μ μνλ μ΄λ¦(μ΄ κ²½μ°μλ t
)μ μ§μ ν μ μκ³ κ°μ²΄λ₯Ό μ°ΎκΈ° μν΄ κ³Όλν μ μ μ°μ§ μμλ λ©λλ€.
μ¬λ¬ κ°μ²΄λ₯Ό export νλ κ²½μ°, μ΅μμ-λ 벨μ λμΈμ (If you're exporting multiple objects, put them all at top-level)
export class SomeType { /* ... */ }
export function someFunc() { /* ... */ }
λ°λλ‘ import ν λ:
import { SomeType, someFunc } from "./MyThings";
let x = new SomeType();
let y = someFunc();
λ§μ κ²μ import νλ κ²½μ°, λ€μμ€νμ΄μ€ import ν¨ν΄μ μ¬μ©νμΈμ (Use the namespace import pattern if you're importing a large number of things)
export class Dog { ... }
export class Cat { ... }
export class Tree { ... }
export class Flower { ... }
import * as myLargeModule from "./MyLargeModule.ts";
let x = new myLargeModule.Dog();
μ’ μ’ λͺ¨λμ κΈ°λ₯μ νμ₯ν΄μΌ ν νμκ° μμ΅λλ€. μΌλ°μ μΈ JS ν¨ν΄μ JQuery νμ₯μ΄ μλνλ λ°©μκ³Ό μ μ¬νκ² *νμ₯(extensions)*μΌλ‘ κΈ°μ‘΄μ κ°μ²΄λ₯Ό 보κ°νλ κ²μ λλ€. μμμ μΈκΈνλ―μ΄ λͺ¨λμ μ μ λ€μμ€νμ΄μ€ κ°μ²΄μ κ°μ΄ λ³ν©(merge) νμ§ μμ΅λλ€. μ¬κΈ°μ μΆμ²νλ λ°©λ²μ κΈ°μ‘΄μ κ°μ²΄λ₯Ό λ³ννμ§ μκ³ μλ‘μ΄ κΈ°λ₯μ μ 곡νλ κ°μ²΄λ₯Ό export νλ κ²μ λλ€.
Calculator.ts
λͺ¨λμ μ μλ κ°λ¨ν κ³μ°κΈ° ꡬνμ μκ°ν΄λ³΄μΈμ.
μ΄ λͺ¨λλ μ
λ ₯ λ¬Έμμ΄ λͺ©λ‘μ μ λ¬νκ³ κ²°κ³Όλ₯Ό μμ±νμ¬ κ³μ°κΈ°μ κΈ°λ₯μ ν
μ€νΈν μ μλ ν¬νΌ ν¨μλ₯Ό export ν©λλ€.
export class Calculator {
private current = 0;
private memory = 0;
private operator: string;
protected processDigit(digit: string, currentValue: number) {
if (digit >= "0" && digit <= "9") {
return currentValue * 10 + (digit.charCodeAt(0) - "0".charCodeAt(0));
}
}
protected processOperator(operator: string) {
if (["+", "-", "*", "/"].indexOf(operator) >= 0) {
return operator;
}
}
protected evaluateOperator(operator: string, left: number, right: number): number {
switch (this.operator) {
case "+": return left + right;
case "-": return left - right;
case "*": return left * right;
case "/": return left / right;
}
}
private evaluate() {
if (this.operator) {
this.memory = this.evaluateOperator(this.operator, this.memory, this.current);
}
else {
this.memory = this.current;
}
this.current = 0;
}
public handleChar(char: string) {
if (char === "=") {
this.evaluate();
return;
}
else {
let value = this.processDigit(char, this.current);
if (value !== undefined) {
this.current = value;
return;
}
else {
let value = this.processOperator(char);
if (value !== undefined) {
this.evaluate();
this.operator = value;
return;
}
}
}
throw new Error(`Unsupported input: '${char}'`);
}
public getResult() {
return this.memory;
}
}
export function test(c: Calculator, input: string) {
for (let i = 0; i < input.length; i++) {
c.handleChar(input[i]);
}
console.log(`result of '${input}' is '${c.getResult()}'`);
}
λ
ΈμΆλ test
ν¨μλ₯Ό μ¬μ©νλ κ°λ¨ν κ³μ°κΈ° ν
μ€νΈμ
λλ€.
import { Calculator, test } from "./Calculator";
let c = new Calculator();
test(c, "1+2*33/11="); // 9 μΆλ ₯
10μ΄ μλ μ«μλ₯Ό μ
λ ₯λ°μ μ μλλ‘ μ΄κ²μ μμνμ¬ ProgrammerCalculator.ts
μ λ§λ€μ΄λ³΄κ² μ΅λλ€.
import { Calculator } from "./Calculator";
class ProgrammerCalculator extends Calculator {
static digits = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"];
constructor(public base: number) {
super();
const maxBase = ProgrammerCalculator.digits.length;
if (base <= 0 || base > maxBase) {
throw new Error(`base has to be within 0 to ${maxBase} inclusive.`);
}
}
protected processDigit(digit: string, currentValue: number) {
if (ProgrammerCalculator.digits.indexOf(digit) >= 0) {
return currentValue * this.base + ProgrammerCalculator.digits.indexOf(digit);
}
}
}
// μλ‘ μμλ calculatorλ₯Ό Calculatorλ‘ export νκΈ°
export { ProgrammerCalculator as Calculator };
// λν ν¬νΌ ν¨μλ export νκΈ°
export { test } from "./Calculator";
μλ‘μ΄ ProgrammerCalculator
λͺ¨λμ Calculator
λͺ¨λκ³Ό μ μ¬ν API ννλ₯Ό export νμ§λ§, μλ λͺ¨λμ κ°μ²΄λ₯Ό 보κ°νμ§λ μμ΅λλ€.
λ€μμ ProgrammerCalculator ν΄λμ€μ λν ν
μ€νΈμ
λλ€:
import { Calculator, test } from "./ProgrammerCalculator";
let c = new Calculator(2);
test(c, "001+010="); // 3 μΆλ ₯
λͺ¨λ κΈ°λ° κ΅¬μ±μ μ²μ μ μ©ν λ, μΌλ°μ μΌλ‘ μΆκ°μ μΈ λ€μμ€νμ΄μ€ κ³μΈ΅μμ exportsλ₯Ό λν νλ κ²½ν₯μ΄ μμ΅λλ€. λͺ¨λμλ μ체 μ€μ½νκ° μμΌλ©°, exportλ μ μΈλ§ λͺ¨λ μΈλΆμμ λ³Ό μ μμ΅λλ€. μ΄λ₯Ό μΌλμ λκ³ λ€μμ€νμ΄μ€λ λͺ¨λμ λ€λ£° λ κ±°μ κ°μ λ³κ²½νμ§ μμ΅λλ€.
κ΅¬μ± μ λ©΄μμ λ€μμ€νμ΄μ€λ λ Όλ¦¬μ μΌλ‘ κ΄λ ¨λ κ°μ²΄μ νμ μ μ μ μ€μ½νλ‘ κ·Έλ£Ήννλλ° νΈλ¦¬ν©λλ€. μλ₯Ό λ€μ΄, C#μ κ²½μ°, System.Collectionsμμ λͺ¨λ 컬λ μ νμ μ μ°Ύμ μ μμ΅λλ€. νμ μ κ³μΈ΅μ λ€μμ€νμ΄μ€λ‘ ꡬμ±νμ¬ ν΄λΉ νμ μ μ¬μ©μμκ² "λ°κ²¬"ν μ μλ μ’μ κ²½νμ μ 곡ν©λλ€. λ°λ©΄, λͺ¨λμ μ΄λ―Έ νμΌ μμ€ν μ λ°λμ μ‘΄μ¬ν©λλ€. κ²½λ‘μ νμΌ μ΄λ¦μΌλ‘ ν΄μνκΈ° μν΄μ, λ Όλ¦¬μ κ΅¬μ± μ²΄κ³λ₯Ό μ¬μ©ν μ μμ΅λλ€. 리μ€νΈ λͺ¨λμ΄ μλ /collections/generic/ ν΄λλ₯Ό μ¬μ©ν μ μμ΅λλ€.
λ€μμ€νμ΄μ€λ μ μ μ€μ½νμμ λ€μ΄λ° μΆ©λμ νΌνκΈ° μν΄ μ€μν©λλ€.
μλ₯Ό λ€μ΄, My.Application.Customer.AddForm
κ³Ό My.Application.Order.AddForm
-- λ νμ
μ μ΄λ¦μ κ°μ§λ§ λ€λ₯Έ λ€μμ€νμ΄μ€λ₯Ό κ°μ§κ³ μμ΅λλ€.
κ·Έλ¬λ μ΄κ²μ λͺ¨λμμ λ¬Έμ κ° λμ§ μμ΅λλ€.
λͺ¨λ λ΄μμ λ κ°μ κ°μ²΄κ° κ°μ μ΄λ¦μ κ°μ§λ§ν μ΄μ λ μμ΅λλ€.
μ¬μ© μΈ‘λ©΄μμ νΉμ λͺ¨λμ μ¬μ©μλ λͺ¨λμ μ°Έμ‘°νλλ° μ¬μ©ν μ΄λ¦μ μ ννλ―λ‘ μ°μ°ν μ΄λ¦ μΆ©λμ λΆκ°λ₯ν©λλ€.
λͺ¨λκ³Ό λ€μμ€νμ΄μ€μ λν μμΈν λ΄μ©μ Namespaces and Modulesλ₯Ό μ°Έκ³ νμΈμ
λ€μμ λͺ¨λ λͺ¨λ ꡬ쑰νμ λν μν μ νΈμ λλ€. λ€μ μ€ νλλΌλ νμΌμ μ μ©λλ κ²½μ° μΈλΆ λͺ¨λμ λ€μμ€νμ΄μ€λ₯Ό λ§λ€λ €κ³ νμ§ μμλμ§ λ€μ νμΈνμΈμ:
- μ€μ§ μ΅μμ-λ 벨 μ μΈλ§
export namespace Foo { ... }
μΈ νμΌ (Foo
λ₯Ό μ κ±°νκ³ λͺ¨λ κ²μ 'μμ' λ λ²¨λ‘ μ΄λνμΈμ) - μ΅μμ-λ 벨 μμΉμ λμΌν
export namespace Foo {
λ₯Ό κ°μ§ μ¬λ¬ νμΌ (νλμFoo
λ‘ κ²°ν©λ κ±°λΌ μκ°νμ§ λ§μΈμ!)