Poul K. Sørensen
Poul K. Sørensen

Reputation: 17530

why use Modules and clases in typescript with AMD compiling?

I am creating a module(not sure if it should be module/class/or none) for my project.

I mostly perfer this way.

///<reference path="..\typings\knockout\knockout.d.ts" />
///<reference path="..\typings\requirejs\require.d.ts"/>
///<reference path="hubs.d.ts" />

//export module SignalRService {
    export var deployHub = $.connection.deployHub;
    export var status = ko.observable();
    export function intialize() {
        $.connection.start()
        .done(() => { this.status("Connection Succesfull") } )
        .fail(() => { this.status("Connection Failed")});

    }

//}

this results in me being able to do as this:

var ko: KnockoutStatic = require('ko');
var hostNodes = ko.observableArray([]);
import signalr = module('SignalRService');

export class AppViewModel {

    ///Properties
    hostNodes = hostNodes;
    error = ko.observable();
    signalr = signalr;
    ///Constructor
    constructor() {

        signalr.intialize();
    }
}

i have directly access to the defined stuff in my "module" from the import. Alternative, if i create it as a typescript module. i have to add:

signalr = signalr.SignalRService;

to get access to my module from the import. Anyone who can enlighten me a little about if i missed out something when i do not declare it as a module in my typescript file?

Upvotes: 1

Views: 452

Answers (1)

outcoldman
outcoldman

Reputation: 11832

From my point of view - you are using two different approaches to define modules in TypeScript (JavaScript).

Modules help you to make sure that you will not have collisions with external libraries. For example, if you will define Common class in your script and in the same time one of your external library will have class with the same name, and both of them will be in Global Context - you will have a problem, one of the implementation will override another.

When you do export module SignalRService and you don't use AMD compilation option for JS - you move all your implementation our of Global Context. When you don't do export module, but do AMD compilation - the problem with Global Context also got fixed. So, we can say - that if you application is based on AMD - you probably don't need to have export module.

Upvotes: 1

Related Questions