Friday, 23 November, 2018 UTC


While not for everybody, TypeScript, as well as other strongly typed languages, have been gaining in popularity. With TypeScript being a superset of JavaScript, using it means transpiling your *.ts files down to pure JavaScript before the V8 engine can understand them. You could watch for file changes and automate the transpiling but sometimes you just want to run your script and get results. This is where ts-node comes in. With ts-node we can skip the fuss and execute our TypScript scripts with ease.

👌 Recommended course ⤵

Wes Bos' Learn Node
Getting Started
To get things started, we need to install typescript and ts-node:
## Via npm $ npm install typescript ts-node ## Via Yarn $ yarn add typescript ts-node 
That’s all there is to getting started. Since ts-node is an executable we can run, there’s nothing to import / require in our scripts.
Speaking of scripts, if you don’t already have a TypeScript project to work with, you can just grab this script to mess around with:
Script: reptile.ts
class Reptile { private reptiles: Array<string> = [ 'Alligator', 'Crocodile', 'Chameleon', 'Komodo Dragon', 'Iguana', 'Salamander', 'Snake', 'Lizard', 'Python', 'Tortoise', 'Turtle', ]; shuffle(): void { for (let i = this.reptiles.length - 1; i > 0; i--) { let j: number = Math.floor(Math.random() * (i + 1)); let temp: string = this.reptiles[i]; this.reptiles[i] = this.reptiles[j]; this.reptiles[j] = temp; } } random(count: number = 1, allowDupes?: boolean): Array<string> { let selected: Array<string> = []; if (!allowDupes && count > this.reptiles.length) { throw new Error(`Can't ensure no dupes for that count`); } for (let i: number = 0; i < count; i++) { if (allowDupes) { // Dupes are cool, so let's just pull random reptiles selected.push(this.reptiles[ Math.floor(Math.random() * this.reptiles.length) ]); } else { // Dupes are no go, shuffle the array and grab a few this.shuffle(); selected = this.reptiles.slice(0, count); } } return selected; } } const reptile = new Reptile(); console.log(`With Dupes: ${reptile.random(10, true)}`); console.log(`And Without: ${reptile.random(10)}`); 
The above script simply pulls random values from an array and is overly complex just for the heck of it.
Running Scripts
Before we get to using ts-node to work some magic, it’s good practice to know what would happen if we were to run a TypeScript script with plain ol’ Node, in case we were to ever run into it in the future.
If we were to run the aforementioned reptile.ts script like so:
$ node reptile.ts 
We would be presented with a lovely SyntaxError: Unexpected identifier on the second line of the file, barking about the private class variable.
Cool, now that we know what not to do and what to expect when we do it, let’s try to run the script again with ts-node:
# Via NPM $ npx ts-node reptile.ts # Via Yarn $ yarn run ts-node reptile.ts 
If all went according to plan, the script not only runs, but will log out two comma separated lists of types of reptiles, one potentially having duplicates and one without.
Oh, and in case you’re wondering about the npx command, it’s a tool that now comes with npm that makes it easy to run binaries that are local to the project from the command line.
Speeding Things Up
Under the hood, ts-node takes your script, does a bunch of semantic checking to ensure everything is on the up and up, and then transpiles your TypeScript to JavaScript.
This is the safest option but if we’re not so worried about TypeScript errors, we can pass in the -T or --transpileOnly flag. This flag tells ts-node to simply transpile down to JavaScript and to not worry about any TypeScript errors.
While it’s not always adviseable to use this flag, because you lose out on what makes TypeScript pretty awesome, there are scenarios where it makes sense, like when you’re just trying to run somebody else’s script or if you’re confident that your editor + linter is catching everything and you’re being mindful enough to listen.
TypeScript REPL
Another added bonus to ts-node is being able to spin up a TypeScript REPL (read-eval-print loop) similar to running node without any options.
This TypeScript REPL allows you to write TypeScript right on the command-line and is super handy for testing something out on a whim.
Similar to how you get to the Node.js REPL, all you need to do is run ts-node without any arguments:
# Via npm $ npx ts-node # Via Yarn $ yarn run ts-node 
And now you can enjoy all of the strictness that TypeScript has to offer, right in your favorite terminal!