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!