Setting Up TypeScript

Before writing real TypeScript you need two things: the compiler, and somewhere to run its output. This lesson installs TypeScript into a project, compiles a first file by hand, and shows the faster ways developers usually run .ts files day to day.

Option 1: no install at all

If you just want to experiment, the official TypeScript Playground at typescriptlang.org/play runs entirely in the browser. It shows type errors as you type, the compiled JavaScript side by side, and lets you switch compiler versions and settings. Every lesson on this site also has an in-page editor for running examples.

Option 2: install it in a project

For real work, TypeScript is installed per project as a development dependency, so everyone on the team uses the same version. You'll need Node.js installed first; check with node -v.

$ terminal
mkdir hello-ts
cd hello-ts
npm init -y
npm install --save-dev typescript
npx tsc --version
Output
Version 7.0.2

--save-dev records TypeScript under devDependencies in package.json, because it's only needed while building, not when the finished JavaScript runs. npx tsc runs the copy installed inside this project rather than any global one.

Since version 7.0 (July 2026), the typescript package contains a native compiler written in Go, which typically builds projects around ten times faster than the older JavaScript-based compiler. The language itself is unchanged, so everything in this course applies whichever version you have.

Why not install globally? npm install -g typescript works, but a global compiler can drift out of step with what a project expects. Projects pin their own version so that "it compiles on my machine" means the same thing for everyone.

Compile your first file

Create index.ts:

TS index.ts
const items: string[] = ["keyboard", "mouse", "monitor"];

for (const item of items) {
  console.log(`Packing ${item}`);
}

Then compile it and run the JavaScript that comes out:

$ terminal
npx tsc index.ts
node index.js
Output
Packing keyboard
Packing mouse
Packing monitor

Running tsc with a file name compiles just that file using default settings and writes index.js next to it. Real projects instead use a tsconfig.json file, created with npx tsc --init, so tsc knows which files to compile and how strict to be — lesson 38 covers it in depth.

Watch out: once a tsconfig.json exists, current TypeScript versions refuse to compile a single named file (tsc index.ts) and report error TS5112: tsconfig.json is present but will not be loaded if files are specified on commandline. Run plain npx tsc to build the project, or add --ignoreConfig if you really want to compile one file with default settings.

Watch mode

Re-running tsc after every change gets tedious. Watch mode keeps the compiler running and rebuilds whenever a file is saved:

$ terminal
npx tsc --watch

It prints any new errors as soon as you save, which makes it a handy companion in a second terminal window.

Running .ts files directly

During development, many people skip the separate compile step. These tools are common:

ToolWhat it doesType-checks?
tsx (npm package)npx tsx index.ts — strips types on the fly and runs the file in NodeNo
Node.js 22.18+ / 23.6+node index.ts — Node can strip erasable type syntax itselfNo
Deno, BunAlternative runtimes that run .ts files nativelyDeno can; Bun doesn't
tsc --noEmitChecks the whole project and writes no filesYes — this is the checker

Notice that the "run it directly" tools generally don't report type errors — they only remove the types. A typical setup is to run code with one of them for speed and run tsc --noEmit (in the editor, a pre-commit hook or CI) to catch type errors.

Editor support

VS Code ships with TypeScript support built in: errors are underlined as you type, hovering a variable shows its type, and F12 jumps to a definition. Other editors (WebStorm, Neovim, Zed, Sublime) use the same TypeScript language service through plugins, so the experience is similar everywhere.

Tip: hover over variables constantly while you're learning. Seeing the type TypeScript has worked out for each value is the quickest way to build intuition for everything in the following lessons.