Typescript Modules

Modules

  • 可以用 as 來改名,例如export { Product, PrintProduct as PrintProductName }

Export

  • 由 export 裡面的物件來決定哪些物件要被開放出去

products.ts

"use strict";

class Product{
    name: string
}

function PrintProduct(source: Product){
    console.log(source.name);
}

export { Product, PrintProduct as PrintProductName }

Import

  • 如果要把全部的東西從 products modules 引用進來,我們可以用 import * as Products from "./products" 來執行,但呼叫 modules 裡面的物件時,前面必須加上 Products 的 prefix

app.ts

"use strict";

import * as Products from "./products"

let myProduct = new Products.Product();
myProduct.name = "IPhone6";

Products.PrintProductName(myProduct);