Module

Convenient way of sharing code between files

Example: Basic Module

Modules are defined with the module keyword, and their exports can be access much like properties of an object

module MyModule {
    export function age(n:number){
        return n / 2;
    };

    export var defaultProp = 20;
}
console.log(MyModule.age(32)); // 16
console.log(MyModule.defaultProp); // 20

Sharing Code ( Import and Export Statements )

Modules are a very good way to share code between files

// file1.ts
export function third(n : number) : number{return n / 3}
export function half(n : number) : number{return n / 3}

// file2.ts
import calc = require(file1');
calc.half(16); // 8
calc.third(24); // 8

Last updated

Was this helpful?