TypeScript Modules - exporting and importing Hello world module


Example

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

Load using directory index

If directory contains file named index.ts it can be loaded using only directory name (for index.ts filename is optional).

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

Example usage of defined modules

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!