All academies
Roadmap/Fundamentals

Level 2

Installing TypeScript

Set up TypeScript in a real project: install the compiler with npm, generate a tsconfig.json, compile your first file, and learn the modern ways to run TypeScript directly during development.

12 min read + quiz

Installing the Compiler

TypeScript is distributed as an npm package called 'typescript', which contains the compiler (tsc) and the language server your editor uses. The recommended approach is to install it as a development dependency of each project, rather than globally. A per-project install pins the exact compiler version in package.json, so every teammate and your CI server check the code with the same rules.

A global install (npm install -g typescript) can be convenient for quick experiments, but it invites 'works on my machine' problems: your global version may be newer or older than the project expects, and different projects often need different versions. Prefer local installs and run the local binary through npx.

After installing, verify the setup by printing the compiler version. If npx tsc --version prints something like 'Version 5.6.2', you are ready to go.

# Create a project and install TypeScript locally
mkdir my-app && cd my-app
npm init -y
npm install --save-dev typescript

# Run the project-local compiler via npx
npx tsc --version
# Version 5.6.2

Install TypeScript as a dev dependency so the whole team uses the same compiler version.

Creating tsconfig.json

Every TypeScript project is configured by a file called tsconfig.json at its root. It tells the compiler which files to include, what JavaScript version to output, where compiled files go, and, most importantly, how strict the type checking should be. Running npx tsc --init generates a starter file with sensible defaults and comments explaining each option.

You do not need to understand every option on day one. The handful that matter most are: 'target' (which JavaScript version to emit, for example ES2020), 'module' (which module system to use), 'outDir' (where compiled .js files are written), 'rootDir' (where your source lives), and 'strict' (the master switch for TypeScript's strongest checks).

The presence of tsconfig.json also marks the folder as a TypeScript project for your editor. Without it, the editor falls back to loose defaults, and running tsc with no arguments will not know which files to compile.

// tsconfig.json - a minimal, solid starting point
{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ESNext",
    "moduleResolution": "bundler",
    "rootDir": "src",
    "outDir": "dist",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true
  },
  "include": ["src"]
}

A minimal tsconfig.json: source in src, output in dist, strict checking on.

Your First Compilation

With the compiler installed and configured, create a file at src/index.ts and run npx tsc. The compiler reads tsconfig.json, checks every file under src, and writes plain JavaScript into dist. If there are type errors, it prints them with file names and line numbers; fix them and run again.

Notice the two distinct steps: tsc produces JavaScript, and node runs that JavaScript. TypeScript itself never executes your code. This separation confuses newcomers who expect tsc to 'run' the program; it only translates and checks it.

It is worth opening the emitted dist/index.js once to see what compilation actually did. For modern targets the output looks almost identical to your source, just with the type annotations stripped, which reinforces that TypeScript is JavaScript plus types.

// src/index.ts
const projectName: string = "TypeScript Academy";
const year: number = 2026;

console.log(projectName + " - " + year);

// Terminal:
//   npx tsc          (compiles src/ into dist/)
//   node dist/index.js
// Output: TypeScript Academy - 2026

Compile with tsc, then execute the emitted JavaScript with node. Two separate steps.

Watch Mode and Direct Runners

Re-running tsc after every edit gets old fast. Watch mode (npx tsc --watch) keeps the compiler running: it recompiles the moment you save a file and reports errors instantly in the terminal. Pair it with node --watch on the output, or use a tool that combines both steps.

For development, many teams skip the manual compile step entirely with a runner like tsx, which executes TypeScript files directly: npx tsx src/index.ts. Modern Node versions (22.6 and later) can even run TypeScript natively by stripping types on the fly. Important caveat: these runners strip types without checking them, so you still need tsc (or your editor) to actually catch type errors.

In real applications you will usually rely on your framework's toolchain. Next.js, Vite, and similar tools compile TypeScript for you and surface errors during development and builds, so your only job is to keep the diagnostics clean.

# Recompile automatically on every save
npx tsc --watch

# Or run TypeScript directly during development
npm install --save-dev tsx
npx tsx src/index.ts

# Type-check WITHOUT emitting any files (great for CI)
npx tsc --noEmit

tsc --noEmit is the standard CI command: check every type, write no files.

Turn On Strict Mode From Day One

The single most important compiler option is 'strict': true. It is an umbrella flag that enables a family of checks, including noImplicitAny (parameters must have known types rather than silently becoming any) and strictNullChecks (null and undefined must be handled explicitly instead of lurking inside every type).

Beginners are sometimes tempted to disable strict mode because it produces more errors. This is backwards: those errors are precisely the bugs TypeScript exists to catch. A non-strict codebase gives a false sense of security, because values can be null or any without warning. Retrofitting strictness onto a large codebase later is far more painful than starting strict.

Everything you learn at TypeScript Academy assumes strict mode. If an example seems to produce an error your setup does not, check that strict is enabled in your tsconfig.json.

// With "strict": false, this compiles - and hides two bugs:
function firstChar(text) {          // text is implicitly 'any'
  return text.charAt(0);
}

// With "strict": true, TypeScript demands clarity:
function firstCharStrict(text: string | null): string {
  if (text === null) {
    return "";                       // null handled explicitly
  }
  return text.charAt(0);
}

Strict mode forces implicit any and unhandled null to the surface, where you can fix them.

Try it yourself

This lesson's example is preloaded in the playground — break it, fix it, extend it.

Open in playground

Check your understanding

Question 1 · Multiple choice

Why is installing TypeScript per-project (as a dev dependency) preferred over a global install?

Question 2 · Multiple choice

What does the command npx tsc --noEmit do?

Question 3 · True or false

Runners like tsx (and Node's built-in type stripping) type-check your code before executing it.

Question 4 · Predict the output

After compiling this src/index.ts with tsc and running node dist/index.js, what is printed?

const appName: string = "Academy";
const version: number = 3;
console.log(appName + " v" + version);

Question 5 · Find the error

With "strict": true in tsconfig.json, this file fails to compile. Why?

function shout(message) {
  return message.toUpperCase() + "!";
}

console.log(shout("hello"));