How to create a Node module in TypeScript

By Ross Huggett, Developer at Design Pandr

Firstly, why would you want to? I had heard about the awesomeness of TypeScript and wanted to give it a shot. I also found that there was a lack of a good tutorial out there so I thought I would write one. For those who don’t know what TypeScript is, it’s a typed superset of JavaScript which basically means you get to use familiar concepts like Interfaces and Classes which, if you come from a classic OOP background like me, the learning curve isn’t so big.

The latest version of Node.js (almost) supports ES2015/ES6, so you can still write OO JavaScript in pure Node, but TypeScript gives you a little more.

Let’s use the good old Mars Rover kata, albeit, stripped down.

Create a directory for your module and initialise it as a Node module.

mkdir mars-rover-typescript
npm init

The next two commands install TypeScript and Typings globally. Typings is the TypeScript definition manager which installs TypeScript definitions. So in my case, I’m using Jasmine for my tests, so when I come to write my tests, I can write them in TypeScript while still using Jasmine functions like ‘describe’.

npm install -g typescript
npm install -g typings
npm install -g jasmine

These commands are similar to npm in that they define in a typings.json file the TypeScript definitions the module will use.

typings install dt~node --global --save
typings install dt~jasmine --global --save-dev

Initialise Jasmine.

jasmine init

The next file you’re going to need is tsconfig.json which configures the TypeScript compiler and root files. I couldn’t figure out if there’s a way to generate this on the command line, so I used my IDE WebStorm to generate it for me, but you could just use the example on the TypeScript website. If you just wanted to get something up and running, you could just use mine:

{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"sourceMap": true,
"outDir": "./built"
},
"exclude": [
"node_modules"
]
}

Note that I’ve configured the ‘outDir’ to ‘./built’. This will be where the complied JavaScript will get built to.

Now install the dependencies.

typings install

You should get a new folder called ‘typings’ with Node and Jasmine definitions in it. You’re now ready to begin development of you module.

Create a test in the spec folder. I’ll call mine ‘MarsRoverSpec.ts’, noting the ‘.ts’ file extension and write your tests and implementation in TypeScript. My implementation will be called ‘MarsRover.ts’.

To build the project, just type:

tsc

This creates the ‘built’ directory and the compiled JavaScript. The module gets exported the usual way and is ready to be imported like any other Node module.

That’s it. I hope you found that useful.

My first impressions of TypeScript are that it’s easy to learn, and coming from a classic OOP background, it was easy to pickup. I found myself leaning heavily on the compiler solely missing when doing classic JavaScript development.

Static type checking made a big difference during development, making refactoring easier. However, there was a clear performance hit.

The docs were short and clear and having JavaScript interoperability was useful. I found I was able to use my IDE more, especially during refactoring.

I had already done this kata before in Java so I had some idea of what it might look like, but I wanted to come into it with a new perspective. Here’s what my implementation looked like in the end.

You can see the full version here, with tests.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.