Hello world 模块

//hello.ts
export function hello(name: string){
    console.log(`Hello ${name}!`);
} 
function helloES(name: string){
    console.log(`Hola ${name}!`);
}
export {helloES};
export default hello;

使用目录索引加载

如果目录包含名为 index.ts 的文件,则只能使用目录名加载(对于 index.ts filename 是可选的)。

//welcome/index.ts
export function welcome(name: string){
    console.log(`Welcome ${name}!`);
}

已定义模块的示例用法

import {hello, helloES} from "./hello";  // load specified elements
import defaultHello from "./hello";      // load default export into name defaultHello
import * as Bundle from "./hello";       // load all exports as Bundle
import {welcome} from "./welcome";       // note index.ts is omitted

hello("World");                          // Hello World!
helloES("Mundo");                        // Hola Mundo!
defaultHello("World");                   // Hello World!

Bundle.hello("World");                   // Hello World!
Bundle.helloES("Mundo");                 // Hola Mundo!

welcome("Human");                        // Welcome Human!