Modules
// triple-slash reference needs no module loading, only references at the top of consuming files
// geometry.ts will transpile into a plain .js
class Square {
constructor(public sideLength: number = 0) {
}
area() {
return Math.pow(this.sideLength, 2);
}
}
// main.ts needs a reference to geometry.ts for the class definition
// this is done by a triple-slash reference
/// <reference path="geometry.ts" />
var square = new Square(5);
// modules can export classes
module Geometry {
export class Square {
constructor(public sideLength: number = 0) {
}
area() {
return Math.pow(this.sideLength, 2);
}
}
}
// '.' can be used as separator for sub modules
var square = new Geometry.Square(5);
// upon import, local alias can be defined for referencing a module
import Geom = Geometry;
var square = new Geom.Square(10);
// imports and exports across files need module loading (AMD, commonJS)
// the chosen loader has to be added to the project stack separately
// geometry.ts will transpile into a module with a syntax depending on the chosen loader
export class Square {
constructor(public sideLength: number = 0) {
}
area() {
return Math.pow(this.sideLength, 2);
}
}
// main.ts needs a reference to the class definition
// this is done by using the same syntax as used in geometry.ts
import { Square as SC } from "./geometry";
square = new SC(4);