Avatar
by Steve Higgs on 31 Jul 2018

We :heart: TypeScript! We first started using it in our frontend JavaScript and really enjoyed the combination of modern JavaScript with optional typing. But can we get the same features when writing backend code in Node.js? (Spoiler: yes, yes we can).

What is TypeScript?

TypeScript is modern JavaScript + optional types. JavaScript is normally a dynamically typed language, this means in the following:


function hello(a) {
    console.log(a);
}

a could be a number, a string or even a complex object. This can be problematic when writing code as we end up with only one way to ensure the code is correct: running it, either under test or gulp in a real environment. While this works it leads to lengthy write/test cycles. Ideally we would like to pick up the obvious errors as we type.

A TypeScript equivalent of the above that only accepts strings would be:

function hello(a: string) {
    console.log(a)
}

The type notation added after a tells the TypeScript compiler that a will always be a string.

The any type

Setting a variable type to any in TypeScript gives us an escape hatch, it lets us tell TypeScript to stop type checking at this point. This is not ideal and is to be avoided where we can but sometimes it is unavoidable. This ability to escape out of TypeScript makes porting over existing codebases much easier.

I’m not going to go into a huge amount of detail of the rest of the features of TypeScript here. I would recommend taking a look at the TypeScript website for more information.

Some simple TypeScript in Node.js

Lets set up a simple “Hello world” app with a function in Node.js.

Hello world in JavaScript

  • Create a new directory
  • Run npm init in the directory to set up the package.json file
  • Create a new file called hello.js

Put the following into hello.js:

function hello(a) {
    console.log(`hello ${a}`)
}

When we run this with node ./hello.js we will see hello world printed to the console.

Hello world in TypeScript

Lets try and rewrite our “hello.js” file into TypeScript:

  • Rename hello.js to hello.ts

We’ll add a type to our code so the contents of hello.ts will look like:

function hello(a:string) {
    console.log(`hello ${a}`)
}

So what happens when we run that?

Unexpected token :

Oops.

This is to be expected, Node.js does not understand TypeScript and so rightly throws an error. Lets teach Node.js about TypeScript.

  • First add TypeScript as a dev dependancy using npm install --save-dev typescript
  • Then add ts-node using npm install --save-dev ts-node

ts-node will transparently compile TypeScript for us at runtime allowing us to directly use TypeScript in Node.js. The only problem is that our initial entry file into the app needs to be in JavaScript.

  • Create a file called index.js and put the following in it.
require('ts-node/register');
require('./hello');

This makes ts-node hook into the Node.js module system and then call your existing TypeScript code. The index.js file acts as the entry point into the rest of your app.

Now if we run node index.js we get the Hello world we were after.

Future work

In future posts I will be covering:

  • Using TypeScript with express
  • Writing tests in TypeScript
  • Doing a clean build to eliminate the memory cost of using ts-node