Search code examples
typescriptamd

TypeScript, AMD and referencing imported classes directly


I call my TSC compiler with "--module amd" parameter.

Let's say I have a file geom.ts that contains

export class Cube { //implementation }

If want to reference the Cube class I do

import geom = require('geom');

var myCuble : geom.Cube;

In ActionScript I was used to reference the imported classes directly - there would be no "geom." before the "Cube", unless there would be a conflict between multiple imports and I needed to state the whole package path. This module-dot-class convention is annoying, is there anything I can do?


Solution

  • From TypeScript 1.5 onwards, you can use the ES6 style import statements:

    import { Cube } from 'geom';
    
    var myCube: Cube;
    

    This will be compiled to older versions of ECMAScript (if you aren't targeting ES6) by transforming the code into:

    var glob_1 = require('glob');
    var x = new glob_1.Example();