Typescript Step by Step
  • Introduction
  • Types
  • Interface
  • Class
  • Module
  • Function
  • What Next?
Powered by GitBook
On this page
  • Example: Basic Module
  • Sharing Code ( Import and Export Statements )
  • More on modules

Was this helpful?

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

More on modules

PreviousClassNextFunction

Last updated 4 years ago

Was this helpful?

https://hemantajax.gitbooks.io/es6-step-by-step/content/import-export.html