22

I'm in the process of migrating a large codebase to Typescript, but wanted to get a good handle on when & where I should be using certain things.

Right now I have some large namespaces in modules:

(function (nameSpace) {

    var privateVariables = '';

    function privateFunctions () { }        

    // Public methods
    nameSpace.someMethodHere = function () { };

}(window.nameSpace = window.nameSpace || {}));

My question is in TypeScript, is there any difference / benefit in me just turning these into just Classes? Or should it just be one large Module with classes (each method) inside of it?

module nameSpace {

    export class someMethodHere {
       // etc
    }

    // more classes (my methods) here ??
}

-OR-

class nameSpace {

    someMethodHere () {
        // code
    }
    // more prototyped methods here
}   

Or should I be setting them up in the same Module way I currently have? I was trying and it just kept giving me errors, not sure how I do nameSpace.someMethodHere inside of a module without a class!

Mark Pieszak - Trilon.io
  • 61,391
  • 14
  • 82
  • 96

2 Answers2

23
  • Consider using a module as a good option for a Singleton (one static object accessible everywhere) where a common usage is for libraries.
  • Consider using a class when you need to create several instances of this class or if you plan to extend this class.

Basically modules are good for packing all your code into one convenient global object while class are smart to structure your code and data representation.

[EDIT] The "internal modules" have been renamed "namespaces" moreover it is now discouraged to use namespaces when you can import modules. A namespace can be partial (i.e. described in many files) and while this is sometimes useful, this approach is too global to be optimised properly by the compiler. Read more here

Flavien Volken
  • 19,196
  • 12
  • 100
  • 133
12

It depends. A module should be a discrete set of "things" rather than just a jumbled collection of disparate classes, so if things seem like they belong together, use a module to group them.

I believe the TypeScript team is waiting to see how people use the language before they publish guidance, but I imagine people will publish a module where they currently publish a script - so jQuery would be a module, each jQuery Plugin would be a module, a testing framework would be a module, and an AOP framework would be a module - for example.

Drax
  • 12,682
  • 7
  • 45
  • 85
Fenton
  • 241,084
  • 71
  • 387
  • 401
  • Agreed, well they are all rather connected. I guess now that I think about it, I'm wondering... does it make more sense to have everything within a Class (all the methods being prototyped), or a Module? I do like how a Module at least doesn't have to instantiated – Mark Pieszak - Trilon.io Nov 06 '12 at 17:59
  • 35
    As a baseline, if you are going to have multiple instances of something with data associated with each instance, `class` is the way to go. If you're just grouping logically-connected sets of stateless functions together, `module` is more appropriate. – Ryan Cavanaugh Nov 06 '12 at 18:33
  • I 100% agree with @RyanCavanaugh on that one. – Fenton Nov 06 '12 at 18:44
  • Great! I'll definitely have to consider which things are stateless or not when implementing all of this. At the moment still upgrading/connecting lots of code (lots of developers with different styles) before making the big switch to Typescript. Excited though. – Mark Pieszak - Trilon.io Nov 06 '12 at 21:25
  • @mcpDESIGNS it's good to find other people as excited as me about TypeScript :) – Fenton Nov 06 '12 at 21:26
  • I mean after years of JS, it's hard not to be... It's so great to see it finally becoming more of a real language, instead of the usual "go ahead and type whatever you want" Javascript haha – Mark Pieszak - Trilon.io Nov 06 '12 at 21:30
  • I think I will just group all modules into one big file, I dislike having to play around with this require js as it is not well integrated in VS 2012 – Nikos Dec 14 '12 at 13:56
  • 1
    I keep going back and forth on class and module when I want a singleton. The module makes it easier to expose common features in a singleton manner, which is similar to what we get with a module and an IIFE in JavaScript. The class has a lot of extra features, but makes it difficult to create private members. I'm currently doing both techniques in my TypeScript course and they both have pros and cons. But currently I am thinking the module technique might be more to my personal liking. – John Papa Dec 19 '12 at 01:45