Search code examples
visual-studiomoduletypescript1.8

Whet is the difference between typescript modules


I have some questions regarding Typescript modules... I am writing an angular 1.5 application in typescript, and i write my files like this:

CarModel.ts

module app.models{
    export class Car{
        type:string;
        color: string;
    }
}

CarsController.ts

module app.controllers{

    import Car = app.Car;

    export class CarsController{
        private car:Car;
        ...
    }
}

but lately I am seeing this type of usage on the internet:

CarModel.ts

export class Car{
    type:string;
    color: string;
}

CarsController.ts

import {Car} from './models';
export class CarController{
    private car:Car;
    ....
} 

Now I would like to know what is the difference apart the syntax? Also what is the difference between :

module app.module1{}

and

declare module app.module1 {}

Please note that I'm using Visual Studio 2015, and my target is ES5.


Solution

  • Your first example is an internal module. This creates a global variable that is used to access the contents. The second is an external module, which doesn't create global variables. External modules require some sort of library, such as systemjs or webpack to make them work on the client side. Internal modules don't.

    Internal modules have been renamed to namespaces to prevent this exact type of confusion. Simply replace the keyword module with namespace.