LearnAI ToolsCareerPractice BuildsPlayDevelopmentContact
Lesson 313 min read

Installing TypeScript & Setup

Install the TypeScript compiler, create a tsconfig.json, run TypeScript files, and set up your editor for the best development experience.

On this page 12 sections

Introduction

Before writing any real TypeScript, you need the compiler installed and a basic project set up. This lesson walks through installing TypeScript with npm, understanding what the compiler actually does, configuring a tsconfig.json, and getting your editor to show type errors as you type.

What You Will Learn
  • The prerequisites you need before installing TypeScript.
  • How to install TypeScript locally per-project (the recommended approach) versus globally.
  • What the tsc compiler does and how to run it.
  • How to create and understand a basic tsconfig.json.
  • How to run .ts files directly during development without a manual compile step.

Prerequisites

TypeScript itself runs on Node.js — the TypeScript compiler is a Node program, distributed as an npm package. Before installing TypeScript, you need Node.js and npm installed on your machine. You can check this by running node --version and npm --version in a terminal; both should print a version number.

Installing TypeScript

You can install TypeScript globally, so the tsc command is available anywhere on your machine, or locally inside a specific project, so everyone working on that project (and your CI pipeline) uses the exact same compiler version. The local, per-project install is the recommended approach for real projects.

# Recommended: install as a dev dependency inside a project
npm install --save-dev typescript
# Alternative: install globally (available as `tsc` anywhere)
npm install -g typescript
# Verify the install
npx tsc --version
Output

Click Run to see what this code prints.

The TypeScript Compiler (tsc)

The compiler, tsc, reads your .ts source files, type-checks them, and emits plain .js files. It is important to remember that TypeScript is never executed directly — browsers and Node.js only ever run the compiled JavaScript output.

# Compile a single file (produces hello.js next to hello.ts)
npx tsc hello.ts
# Run the compiled output with Node
node hello.js

Creating a tsconfig.json

Real projects almost always use a tsconfig.json file, which tells the compiler which files to include and how to compile them, so you can just run tsc with no arguments. Generate a starter file with npx tsc --init, then trim it down to the essentials.

{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "Bundler",
"outDir": "dist",
"rootDir": "src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true
},
"include": ["src"]
}

target controls which JavaScript version is emitted, strict turns on the full family of strict type-checking flags (highly recommended for every new project), outDir/rootDir control where compiled files go, and include limits compilation to your source folder.

Running TypeScript Directly

During development, manually running tsc then node for every change is slow. Tools like ts-node and the newer, faster tsx compile and run a .ts file in one step, in memory, without writing a .js file to disk — ideal for scripts and quick iteration. Frameworks like Next.js, Vite, and NestJS bundle equivalent on-the-fly TypeScript support so you never call tsc manually during day-to-day development.

npm install -D tsx
npx tsx hello.ts

Setting Up Your Editor

Visual Studio Code has TypeScript support built in out of the box — unsurprising, since both are maintained by Microsoft and share the same underlying language service. Opening a .ts file automatically gives you inline red squiggles for type errors, hover-to-see-type tooltips, autocomplete, and safe rename-across-files refactoring, with no extension required.

Common Mistakes

Avoid These Mistakes
  • Editing a .ts file, forgetting to recompile, and then wondering why node file.js still shows the old behavior.
  • Only ever installing TypeScript globally — this causes version mismatches between your machine, teammates, and CI.
  • Skipping "strict": true at project start and having to retrofit strict checks onto a much larger codebase later.
  • Confusing ts-node/tsx (development conveniences) with a production runtime — compiled projects are still typically deployed as plain, pre-compiled JavaScript.

Best Practices

  • Install TypeScript as a devDependency in every project rather than relying solely on a global install.
  • Start every new tsconfig.json with "strict": true — it is far easier to keep strict mode on than to add it later.
  • Add a "build": "tsc" (or similar) script to package.json so compiling is a single documented command for the whole team.
  • Use VS Code, or another editor with a real TypeScript language server, so you see type errors immediately rather than only at compile time.

Frequently Asked Questions

Yes — the tsc compiler is itself a Node.js program, so Node and npm must be installed before you can install and run TypeScript.

No. Browsers only understand JavaScript, so TypeScript must always be compiled, or transpiled on the fly by a bundler like Vite or webpack, before it can run.

tsc compiles .ts files into persistent .js files on disk; ts-node (and tools like tsx) compile and execute a .ts file in memory in one step, mainly for development convenience.

You still add typescript as a devDependency, but frameworks like Next.js and Vite handle the actual compiling for you automatically — no manual tsc step is needed during development.

Key Takeaways

  • TypeScript is installed via npm and requires Node.js as a prerequisite.
  • Install TypeScript locally per-project (devDependency) rather than only globally.
  • The tsc compiler type-checks your code and emits plain JavaScript — TypeScript never runs directly.
  • A tsconfig.json configures compiler options like target, strict mode, and output directories.
  • Tools like ts-node and tsx let you run .ts files directly during development without a manual build step.

Summary

You now have TypeScript installed, understand what the compiler does, and have a working tsconfig.json. With your environment ready, it's time to start learning TypeScript's type system itself, beginning with the basic types.

Next Lesson →

Basic Types