在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ HTML/ 介紹
初始化項(xiàng)目結(jié)構(gòu)
聯(lián)合類型
介紹
介紹
介紹
編譯選項(xiàng)
TypeScript 1.6
介紹
介紹
發(fā)展路線圖
介紹
在MSBuild里使用編譯選項(xiàng)
可迭代性
TypeScript 1.3
介紹
介紹
TypeScript 1.1
變量聲明
即將到來的Angular 2框架是使用TypeScript開發(fā)的。 因此Angular和TypeScript一起使用非常簡(jiǎn)單方便
tsconfig.json
介紹
介紹
介紹
在MSBuild里使用編譯選項(xiàng)
使用TypeScript的每日構(gòu)建版本
新建工程
枚舉
三斜線指令
結(jié)合ASP.NET v5使用TypeScript
TypeScript里的this
介紹
TypeScript 1.4
編碼規(guī)范
介紹
模塊解析
ASP.NET 4
架構(gòu)概述
介紹
介紹
ASP.NET Core
TypeScript 1.8
介紹
介紹
創(chuàng)建簡(jiǎn)單工程
TypeScript 1.7
TypeScript 1.5
NPM包的類型
支持TypeScript的編輯器

介紹

從ECMAScript 2015開始,JavaScript引入了模塊的概念。TypeScript也沿用這個(gè)概念。

模塊在其自身的作用域里執(zhí)行,而不是在全局作用域里;這意味著定義在一個(gè)模塊里的變量,函數(shù),類等等在模塊外部是不可見的,除非你明確地使用export形式之一導(dǎo)出它們。 相反,如果想使用其它模塊導(dǎo)出的變量,函數(shù),類,接口等的時(shí)候,你必須要導(dǎo)入它們,可以使用import形式之一。

模塊是自聲明的;兩個(gè)模塊之間的關(guān)系是通過在文件級(jí)別上使用imports和exports建立的。

模塊使用模塊加載器去導(dǎo)入其它的模塊。 在運(yùn)行時(shí),模塊加載器的作用是在執(zhí)行此模塊代碼前去查找并執(zhí)行這個(gè)模塊的所有依賴。 大家最熟知的JavaScript模塊加載器是服務(wù)于Node.js的CommonJS和服務(wù)于Web應(yīng)用的Require.js。

TypeScript與ECMAScript 2015一樣,任何包含頂級(jí)import或者export的文件都被當(dāng)成一個(gè)模塊。

導(dǎo)出

導(dǎo)出聲明

任何聲明(比如變量,函數(shù),類,類型別名或接口)都能夠通過添加export關(guān)鍵字來導(dǎo)出。

Validation.ts
export interface StringValidator {
    isAcceptable(s: string): boolean;
}
ZipCodeValidator.ts
export const numberRegexp = /^[0-9]+$/;

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

導(dǎo)出語句

導(dǎo)出語句很便利,因?yàn)槲覀兛赡苄枰獙?duì)導(dǎo)出的部分重命名,所以上面的例子可以這樣改寫:

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

重新導(dǎo)出

我們經(jīng)常會(huì)去擴(kuò)展其它模塊,并且只導(dǎo)出那個(gè)模塊的部分內(nèi)容。 重新導(dǎo)出功能并不會(huì)在當(dāng)前模塊導(dǎo)入那個(gè)模塊或定義一個(gè)新的局部變量。

ParseIntBasedZipCodeValidator.ts
export class ParseIntBasedZipCodeValidator {
    isAcceptable(s: string) {
        return s.length === 5 && parseInt(s).toString() === s;
    }
}

// 導(dǎo)出原先的驗(yàn)證器但做了重命名
export {ZipCodeValidator as RegExpBasedZipCodeValidator} from "./ZipCodeValidator";

或者一個(gè)模塊可以包裹多個(gè)模塊,并把他們導(dǎo)出的內(nèi)容聯(lián)合在一起通過語法:export * from "module"。

AllValidators.ts
export * from "./StringValidator"; // exports interface StringValidator
export * from "./LettersOnlyValidator"; // exports class LettersOnlyValidator
export * from "./ZipCodeValidator";  // exports class ZipCodeValidator

導(dǎo)入

模塊的導(dǎo)入操作與導(dǎo)出一樣簡(jiǎn)單。 可以使用以下import形式之一來導(dǎo)入其它模塊中的導(dǎo)出內(nèi)容。

導(dǎo)入一個(gè)模塊中的某個(gè)導(dǎo)出內(nèi)容

import { ZipCodeValidator } from "./ZipCodeValidator";

let myValidator = new ZipCodeValidator();

可以對(duì)導(dǎo)入內(nèi)容重命名

import { ZipCodeValidator as ZCV } from "./ZipCodeValidator";
let myValidator = new ZCV();

將整個(gè)模塊導(dǎo)入到一個(gè)變量,并通過它來訪問模塊的導(dǎo)出部分

import * as validator from "./ZipCodeValidator";
let myValidator = new validator.ZipCodeValidator();

具有副作用的導(dǎo)入模塊

盡管不推薦這么做,一些模塊會(huì)設(shè)置一些全局狀態(tài)供其它模塊使用。 這些模塊可能沒有任何的導(dǎo)出或用戶根本就不關(guān)注它的導(dǎo)出。 使用下面的方法來導(dǎo)入這類模塊:

import "./my-module.js";

默認(rèn)導(dǎo)出

每個(gè)模塊都可以有一個(gè)default導(dǎo)出。 默認(rèn)導(dǎo)出使用default關(guān)鍵字標(biāo)記;并且一個(gè)模塊只能夠有一個(gè)default導(dǎo)出。 需要使用一種特殊的導(dǎo)入形式來導(dǎo)入default導(dǎo)出。

default導(dǎo)出十分便利。 比如,像JQuery這樣的類庫(kù)可能有一個(gè)默認(rèn)導(dǎo)出jQuery$,并且我們基本上也會(huì)使用同樣的名字jQuery$導(dǎo)出JQuery。

JQuery.d.ts
declare let $: JQuery;
export default $;
App.ts
import $ from "JQuery";

$("button.continue").html( "Next Step..." );

類和函數(shù)聲明可以直接被標(biāo)記為默認(rèn)導(dǎo)出。 標(biāo)記為默認(rèn)導(dǎo)出的類和函數(shù)的名字是可以省略的。

ZipCodeValidator.ts
export default class ZipCodeValidator {
    static numberRegexp = /^[0-9]+$/;
    isAcceptable(s: string) {
        return s.length === 5 && ZipCodeValidator.numberRegexp.test(s);
    }
}
Test.ts
import validator from "./ZipCodeValidator";

let myValidator = new validator();

或者

StaticZipCodeValidator.ts
const numberRegexp = /^[0-9]+$/;

export default function (s: string) {
    return s.length === 5 && numberRegexp.test(s);
}
Test.ts
import validate from "./StaticZipCodeValidator";

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

// Use function validate
strings.forEach(s => {
  console.log(`"${s}" ${validate(s) ? " matches" : " does not match"}`);
});

default導(dǎo)出也可以是一個(gè)值

OneTwoThree.ts
export default "123";
Log.ts
import num from "./OneTwoThree";

console.log(num); // "123"

export =import = require()

CommonJS和AMD都有一個(gè)exports對(duì)象的概念,它包含了一個(gè)模塊的所有導(dǎo)出內(nèi)容。

它們也支持把exports替換為一個(gè)自定義對(duì)象。 默認(rèn)導(dǎo)出就好比這樣一個(gè)功能;然而,它們卻并不相互兼容。 TypeScript模塊支持export =語法以支持傳統(tǒng)的CommonJS和AMD的工作流模型。

export =語法定義一個(gè)模塊的導(dǎo)出對(duì)象。 它可以是類,接口,命名空間,函數(shù)或枚舉。

若要導(dǎo)入一個(gè)使用了export =的模塊時(shí),必須使用TypeScript提供的特定語法import let = require("module")。

ZipCodeValidator.ts
let numberRegexp = /^[0-9]+$/;
class ZipCodeValidator {
    isAcceptable(s: string) {
        return s.length === 5 && numberRegexp.test(s);
    }
}
export = ZipCodeValidator;
Test.ts
import zip = require("./ZipCodeValidator");

// Some samples to try
let strings = ["Hello", "98052", "101"];

// Validators to use
let validator = new zip();

// Show whether each string passed each validator
strings.forEach(s => {
  console.log(`"${ s }" - ${ validator.isAcceptable(s) ? "matches" : "does not match" }`);
});

生成模塊代碼

根據(jù)編譯時(shí)指定的模塊目標(biāo)參數(shù),編譯器會(huì)生成相應(yīng)的供Node.js (CommonJS),Require.js (AMD),isomorphic (UMD), SystemJSECMAScript 2015 native modules (ES6)模塊加載系統(tǒng)使用的代碼。 想要了解生成代碼中define,requireregister的意義,請(qǐng)參考相應(yīng)模塊加載器的文檔。

下面的例子說明了導(dǎo)入導(dǎo)出語句里使用的名字是怎么轉(zhuǎn)換為相應(yīng)的模塊加載器代碼的。

SimpleModule.ts
import m = require("mod");
export let t = m.something + 1;
AMD / RequireJS SimpleModule.js
define(["require", "exports", "./mod"], function (require, exports, mod_1) {
    exports.t = mod_1.something + 1;
});
CommonJS / Node SimpleModule.js
let mod_1 = require("./mod");
exports.t = mod_1.something + 1;
UMD SimpleModule.js
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        let 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) {
    let mod_1 = require("./mod");
    exports.t = mod_1.something + 1;
});
System SimpleModule.js
System.register(["./mod"], function(exports_1) {
    let mod_1;
    let t;
    return {
        setters:[
            function (mod_1_1) {
                mod_1 = mod_1_1;
            }],
        execute: function() {
            exports_1("t", t = mod_1.something + 1);
        }
    }
});
Native ECMAScript 2015 modules SimpleModule.js
import { something } from "./mod";
export let t = something + 1;

簡(jiǎn)單示例

下面我們來整理一下前面的驗(yàn)證器實(shí)現(xiàn),每個(gè)模塊只有一個(gè)命名的導(dǎo)出。

為了編譯,我們必需要在命令行上指定一個(gè)模塊目標(biāo)。對(duì)于Node.js來說,使用--module commonjs; 對(duì)于Require.js來說,使用`--module amd。比如:

tsc --module commonjs Test.ts

編譯完成后,每個(gè)模塊會(huì)生成一個(gè)單獨(dú)的.js文件。 好比使用了reference標(biāo)簽,編譯器會(huì)根據(jù)import語句編譯相應(yīng)的文件。

Validation.ts
export interface StringValidator {
    isAcceptable(s: string): boolean;
}
LettersOnlyValidator.ts
import { StringValidator } from "./Validation";

const lettersRegexp = /^[A-Za-z]+$/;

export class LettersOnlyValidator implements StringValidator {
    isAcceptable(s: string) {
        return lettersRegexp.test(s);
    }
}
ZipCodeValidator.ts
import { StringValidator } from "./Validation";

const numberRegexp = /^[0-9]+$/;

export class ZipCodeValidator implements StringValidator {
    isAcceptable(s: string) {
        return s.length === 5 && numberRegexp.test(s);
    }
}
Test.ts
import { StringValidator } from "./Validation";
import { ZipCodeValidator } from "./ZipCodeValidator";
import { LettersOnlyValidator } from "./LettersOnlyValidator";

// Some samples to try
let strings = ["Hello", "98052", "101"];

// Validators to use
let validators: { [s: string]: StringValidator; } = {};
validators["ZIP code"] = new ZipCodeValidator();
validators["Letters only"] = new LettersOnlyValidator();

// Show whether each string passed each validator
strings.forEach(s => {
    for (let name in validators) {
        console.log(`"${ s }" - ${ validators[name].isAcceptable(s) ? "matches" : "does not match" } ${ name }`);
    }
});

可選的模塊加載和其它高級(jí)加載場(chǎng)景

有時(shí)候,你只想在某種條件下才加載某個(gè)模塊。 在TypeScript里,使用下面的方式來實(shí)現(xiàn)它和其它的高級(jí)加載場(chǎng)景,我們可以直接調(diào)用模塊加載器并且可以保證類型完全。

編譯器會(huì)檢測(cè)是否每個(gè)模塊都會(huì)在生成的JavaScript中用到。 如果一個(gè)模塊標(biāo)識(shí)符只在類型注解部分使用,并且完全沒有在表達(dá)式中使用時(shí),就不會(huì)生成require這個(gè)模塊的代碼。 省略掉沒有用到的引用對(duì)性能提升是很有益的,并同時(shí)提供了選擇性加載模塊的能力。

這種模式的核心是import id = require("...")語句可以讓我們?cè)L問模塊導(dǎo)出的類型。 模塊加載器會(huì)被動(dòng)態(tài)調(diào)用(通過require),就像下面if代碼塊里那樣。 它利用了省略引用的優(yōu)化,所以模塊只在被需要時(shí)加載。 為了讓這個(gè)模塊工作,一定要注意import定義的標(biāo)識(shí)符只能在表示類型處使用(不能在會(huì)轉(zhuǎn)換成JavaScript的地方)。

為了確保類型安全性,我們可以使用typeof關(guān)鍵字。 typeof關(guān)鍵字,當(dāng)在表示類型的地方使用時(shí),會(huì)得出一個(gè)類型值,這里就表示模塊的類型。

示例:Node.js里的動(dòng)態(tài)模塊加載
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("...")) { /* ... */ }
}
示例:require.js里的動(dòng)態(tài)模塊加載
declare function require(moduleNames: string[], onLoad: (...args: any[]) => void): void;

import { ZipCodeValidator as Zip } from "./ZipCodeValidator";

if (needZipValidation) {
    require(["./ZipCodeValidator"], (ZipCodeValidator: typeof Zip) => {
        let validator = new ZipCodeValidator();
        if (validator.isAcceptable("...")) { /* ... */ }
    });
}
示例:System.js里的動(dòng)態(tài)模塊加載
declare let System: any;

import { ZipCodeValidator as Zip } from "./ZipCodeValidator";

if (needZipValidation) {
    System.import("./ZipCodeValidator").then((ZipCodeValidator: typeof Zip) => {
        let x = new ZipCodeValidator();
        if (x.isAcceptable("...")) { /* ... */ }
    });
}

使用其它的JavaScript庫(kù)

為了描述不是用TypeScript編寫的類庫(kù)的類型,我們需要聲明類庫(kù)導(dǎo)出的API。

我們叫它聲明因?yàn)樗皇峭獠砍绦虻木唧w實(shí)現(xiàn)。 通常會(huì)在.d.ts里寫這些定義。 如果你熟悉C/C++,你可以把它們當(dāng)做.h文件。 讓我們看一些例子。

外部模塊

在Node.js里大部分工作是通過加載一個(gè)或多個(gè)模塊實(shí)現(xiàn)的。 我們可以使用頂級(jí)的export聲明來為每個(gè)模塊都定義一個(gè).d.ts文件,但最好還是寫在一個(gè)大的.d.ts文件里。 我們使用與構(gòu)造一個(gè)外部命名空間相似的方法,但是這里使用module關(guān)鍵字并且把名字用引號(hào)括起來,方便之后import。 例如:

node.d.ts (simplified excerpt)
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 let sep: string;
}

現(xiàn)在我們可以/// <reference> node.d.ts并且使用import url = require("url");加載模塊。

/// <reference path="node.d.ts"/>
import * as URL from "url";
let myUrl = URL.parse("http://www.typescriptlang.org");

創(chuàng)建模塊結(jié)構(gòu)指導(dǎo)

盡可能地在頂層導(dǎo)出

用戶應(yīng)該更容易地使用你模塊導(dǎo)出的內(nèi)容。 嵌套層次過多會(huì)變得難以處理,因此仔細(xì)考慮一下如何組織你的代碼。

從你的模塊中導(dǎo)出一個(gè)命名空間就是一個(gè)增加嵌套的例子。 雖然命名空間有時(shí)候有它們的用處,在使用模塊的時(shí)候它們額外地增加了一層。 這對(duì)用戶來說是很不便的并且通常是多余的。

導(dǎo)出類的靜態(tài)方法也有同樣的問題 - 這個(gè)類本身就增加了一層嵌套。 除非它能方便表述或便于清晰使用,否則請(qǐng)考慮直接導(dǎo)出一個(gè)輔助方法。

如果僅導(dǎo)出單個(gè) classfunction,使用 export default

就像“在頂層上導(dǎo)出”幫助減少用戶使用的難度,一個(gè)默認(rèn)的導(dǎo)出也能起到這個(gè)效果。 如果一個(gè)模塊就是為了導(dǎo)出特定的內(nèi)容,那么你應(yīng)該考慮使用一個(gè)默認(rèn)導(dǎo)出。 這會(huì)令模塊的導(dǎo)入和使用變得些許簡(jiǎn)單。 比如:

MyClass.ts

export default class SomeType {
  constructor() { ... }
}

MyFunc.ts

export default function getThing() { return 'thing'; }

Consumer.ts

import t from "./MyClass";
import f from "./MyFunc";
let x = new t();
console.log(f());

對(duì)用戶來說這是最理想的。他們可以隨意命名導(dǎo)入模塊的類型(本例為t)并且不需要多余的(.)來找到相關(guān)對(duì)象。

如果要導(dǎo)出多個(gè)對(duì)象,把它們放在頂層里導(dǎo)出

MyThings.ts

export class SomeType { /* ... */ }
export function someFunc() { /* ... */ }

相反地,當(dāng)導(dǎo)入的時(shí)候:

明確地列出導(dǎo)入的名字

Consumer.ts

import { SomeType, SomeFunc } from "./MyThings";
let x = new SomeType();
let y = someFunc();

使用命名空間導(dǎo)入模式當(dāng)你要導(dǎo)出大量?jī)?nèi)容的時(shí)候

MyLargeModule.ts

export class Dog { ... }
export class Cat { ... }
export class Tree { ... }
export class Flower { ... }

Consumer.ts

import * as myLargeModule from "./MyLargeModule.ts";
let x = new myLargeModule.Dog();

使用重新導(dǎo)出進(jìn)行擴(kuò)展

你可能經(jīng)常需要去擴(kuò)展一個(gè)模塊的功能。 JS里常用的一個(gè)模式是JQuery那樣去擴(kuò)展原對(duì)象。 如我們之前提到的,模塊不會(huì)像全局命名空間對(duì)象那樣去合并。 推薦的方案是不要去改變?cè)瓉淼膶?duì)象,而是導(dǎo)出一個(gè)新的實(shí)體來提供新的功能。

假設(shè)Calculator.ts模塊里定義了一個(gè)簡(jiǎn)單的計(jì)算器實(shí)現(xiàn)。 這個(gè)模塊同樣提供了一個(gè)輔助函數(shù)來測(cè)試計(jì)算器的功能,通過傳入一系列輸入的字符串并在最后給出結(jié)果。

Calculator.ts

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 handelChar(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.handelChar(input[i]);
    }

    console.log(`result of '${input}' is '${c.getResult()}'`);
}

這是使用導(dǎo)出的test函數(shù)來測(cè)試計(jì)算器。

TestCalculator.ts

import { Calculator, test } from "./Calculator";

let c = new Calculator();
test(c, "1+2*33/11="); // prints 9

現(xiàn)在擴(kuò)展它,添加支持輸入其它進(jìn)制(十進(jìn)制以外),讓我們來創(chuàng)建ProgrammerCalculator.ts。

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();
        if (base <= 0 || base > ProgrammerCalculator.digits.length) {
            throw new Error("base has to be within 0 to 16 inclusive.");
        }
    }

    protected processDigit(digit: string, currentValue: number) {
        if (ProgrammerCalculator.digits.indexOf(digit) >= 0) {
            return currentValue * this.base + ProgrammerCalculator.digits.indexOf(digit);
        }
    }
}

// Export the new extended calculator as Calculator
export { ProgrammerCalculator as Calculator };

// Also, export the helper function
export { test } from "./Calculator";

新的ProgrammerCalculator模塊導(dǎo)出的API與原先的Calculator模塊很相似,但卻沒有改變?cè)K里的對(duì)象。 下面是測(cè)試ProgrammerCalculator類的代碼:

TestProgrammerCalculator.ts

import { Calculator, test } from "./ProgrammerCalculator";

let c = new Calculator(2);
test(c, "001+010="); // prints 3

模塊里不要使用命名空間

當(dāng)初次進(jìn)入基于模塊的開發(fā)模式時(shí),可能總會(huì)控制不住要將導(dǎo)出包裹在一個(gè)命名空間里。 模塊具有其自己的作用域,并且只有導(dǎo)出的聲明才會(huì)在模塊外部可見。 記住這點(diǎn),命名空間在使用模塊時(shí)幾乎沒什么價(jià)值。

在組織方面,命名空間對(duì)于在全局作用域內(nèi)對(duì)邏輯上相關(guān)的對(duì)象和類型進(jìn)行分組是很便利的。 例如,在C#里,你會(huì)從System.Collections里找到所有集合的類型。 通過將類型有層次地組織在命名空間里,可以方便用戶找到與使用那些類型。 然而,模塊本身已經(jīng)存在于文件系統(tǒng)之中,這是必須的。 我們必須通過路徑和文件名找到它們,這已經(jīng)提供了一種邏輯上的組織形式。 我們可以創(chuàng)建/collections/generic/文件夾,把相應(yīng)模塊放在這里面。

命名空間對(duì)解決全局作用域里命名沖突來說是很重要的。 比如,你可以有一個(gè)My.Application.Customer.AddFormMy.Application.Order.AddForm -- 兩個(gè)類型的名字相同,但命名空間不同。 然而,這對(duì)于模塊來說卻不是一個(gè)問題。 在一個(gè)模塊里,沒有理由兩個(gè)對(duì)象擁有同一個(gè)名字。 從模塊的使用角度來說,使用者會(huì)挑出他們用來引用模塊的名字,所以也沒有理由發(fā)生重名的情況。

更多關(guān)于模塊和命名空間的資料查看[命名空間和模塊](./Namespaces and Modules.md)

危險(xiǎn)信號(hào)

以下均為模塊結(jié)構(gòu)上的危險(xiǎn)信號(hào)。重新檢查以確保你沒有在對(duì)模塊使用命名空間:

  • 文件的頂層聲明是export namespace Foo { ... } (刪除Foo并把所有內(nèi)容向上層移動(dòng)一層)
  • 文件只有一個(gè)export classexport function (考慮使用export default
  • 多個(gè)文件的頂層具有同樣的export namespace Foo { (不要以為這些會(huì)合并到一個(gè)Foo中?。?/li>
上一篇:介紹下一篇:編譯選項(xiàng)