TypeScript allows for better JavaScript development. JavaScript is getting useful in more domains. But different systems require different ways of handling modules and exposing features. TypeScript solves this by compiling differently when a target is specified.

But what if you need a TypeScript script that supports both vanilla browser JS and Node.js? What if you need to expose 10+ classes?


Creating a different way of handling the exposure¬†of objects¬†might be the solution.¬†I’ve created a small script to help.¬†Let’s say we need to expose class EventDispatcher¬†and method createEventDispatcher. Add them both to the¬†exportables array. The script¬†will try to detect the right way of exposing the objects.

With¬†a declare for module and¬†defined¬†you don’t have to add extra typings to your project for NodeJS of AMD. The right method will be available when the JS is compiled and executed.

The exportables array is wrapped in an anonymous function to prevent collisions with other scripts.

Node.js support

Node uses module.exports to expose methods and classes to other scripts. The script will try to resolve the name of each object in the exportable array and add it as a property to the modules.exports.

To keep things strongly typed, consider defining an interface for the exports:

Use the interface with the require:

The events variable is now strongly typed.

AMD support

The script supports AMD. It registers each object as a function through the require method.


Not using modules? Great! Just add a reference & use it:

If all else fails, it will add the object to the window object. This is especially useful when you use your own module system or way of information hiding.

Example: Mocha, Chai in browser

So I’m using Mocha, Chai and NPM to test. Everything works fine with an npm test.¬†But I want the tests to be available in the browser as well. I don’t like to compile differently and I should not have too. The following JavaScript combines both worlds in one test solution: