Paul Redmond Paul Redmond - 2 months ago 11
TypeScript Question

TypeScript - Use modules or classes with unit testing in mind?

Which strategy of content architecture is best with unit testing in mind in TypeScript? Creating modules or classes?

module:

moduleX.method1();
// Exported method

class:
var x = moduleX.method1();
// Public method

Answer

best with unit testing in mind in TypeScript

If you have a function that doesn't depend on local state in a class .... then it should just be a function. No need to new SomeClass().foo() when you can foo()

Here you can use namespaces to offer a nice consolidation of different function into a single object : https://basarat.gitbooks.io/typescript/content/docs/project/namespaces.html and you get something like Utils.foo()

Be warned that I would prefer you just make a javascript module instead of a namespace : https://basarat.gitbooks.io/typescript/content/docs/project/modules.html

Most importantly have fun

Comments