Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

Running TypeScript: tsc, ts-node and the Playground

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.

The shortcut

Ask "Who is this for, and how long will it live?"

  • Just me, for thirty seconds → Playground.
  • Just me, while I work → a runner.
  • Users, for months → compile with tsc, run the JavaScript.
One script, three momentsStep 1 of 4
  1. Unsure about a type

    You are not sure what [1, "a"] is inferred as. Paste it into the Playground and hover: (string | number)[].

The trap: relying on a type-stripping runner and never running tsc. Your code runs, but nobody is checking the types anymore. Keep npx tsc --noEmit (check only, write nothing) in your workflow.

Drills

Answer fast — trust the rule.

quick drill

You want to see what JavaScript a small snippet compiles to, without installing anything. What do you use?

quick drill

What does npx ts-node src/overdue.ts do?

quick drill

Which is the usual way to run a TypeScript server in production?

quick drill

A runner only strips types. Your file assigns a string to a number variable. What happens?

Remember

  • Experiment in the Playground, iterate with a runner, ship compiled JavaScript.
  • Ask who it is for and how long it lives to choose the method.
  • Trap: running is not checking — keep tsc --noEmit in your workflow.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainPick the right way to run it

For each situation, say whether you would use the TS Playground, ts-node (or a similar runner), or tsc followed by node, and explain why: (a) checking what a tricky type resolves to, (b) running a quick script during development, (c) shipping a server to production.