Skip to the workspace
Pebellslearn by building
Introduction to TypeScript  ›  Lesson

Running TypeScript: tsc, ts-node and the Playground

Running TypeScript 12 minYou can run TypeScript by compiling it to JavaScript first, by using a runner such as ts-node that compiles on the fly, or by trying snippets in the online Playground.

typescript / Running TypeScript: tsc, ts-node and the Playground

Explore each scene at your own pace.

01 · The rule

The rule

Running TypeScript: tsc, ts-node and the Playground

You can run TypeScript by compiling it to JavaScript first, by using a runner such as ts-node that compiles on the fly, or by trying snippets in the online Playground.

Follow a file through checking, emitted JavaScript and execution in the appropriate environment.

You wrote a ten-line script to print overdue tasks. Do you really need a build step to see its output? Not always. There are three common ways to run TypeScript, and each fits a different moment.

The rule

Experiment in the Playground, iterate with a runner, ship compiled JavaScript.

1. The TS Playground. An official web page where you type TypeScript on one side and see the compiled JavaScript and any errors on the other. Nothing to install. Hover over a variable to see its type, and change compiler settings from a menu. It is ideal for questions like "what type does this expression have?"

2. A runner such as ts-node. ts-node compiles your file in memory and runs it with Node.js in one step:

npm install --save-dev ts-node
npx ts-node src/overdue.ts

No .js file is written to disk. That makes it handy for scripts and quick checks while developing. Other runners, such as tsx, work in a similar way, and recent versions of Node.js can run many .ts files directly by stripping out the type annotations. Note that stripping types is not the same as checking them: a runner that only strips will happily run code that has type errors.

3. Compile, then run. The production route:

npx tsc              # writes dist/overdue.js
node dist/overdue.js

The compiler checks everything, writes JavaScript once, and Node runs that output. The running program never needs TypeScript installed, and it starts without any compile step.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.