Setting up TypeScript

Topic 2 of 3 in Getting started7 min readIntermediateFree course

Overview

A TypeScript project needs three things: TypeScript itself (the checker), a tsconfig.json (its settings), and a way to run the code. In 2026 the running part has become much simpler, because Node.js can run TypeScript files directly. This lesson builds a small project from an empty folder.

You need Node.js 22.18 or newer — Node 26 LTS is recommended. Check with node --version. If you need to install or upgrade Node, see our Node.js course or nodejs.org.

The tools and what they do

The tools around TypeScript. The editor (VS Code) shows red squiggles as you type, plus autocomplete and rename — it checks types. tsc –noEmit checks the whole project; run it before commits and in CI — it checks. node app.ts runs TypeScript directly by stripping the types — no check. Vite, esbuild, Bun and similar tools bundle or run .ts files fast by stripping types — no check. tsc with emit checks AND writes .js and .d.ts files to publish — it checks. Only the type checker finds type errors; tools that just strip types run your code even when it has errors.

The key point: running and checking are separate jobs. Fast tools such as Node, Vite and Vitest simply remove the types and run the JavaScript — even if the types are wrong. Only tsc (and your editor, which uses the same checker) actually finds type errors.

Step 1: create the project

Terminal
mkdir ts-hello
cd ts-hello
npm init -y
npm pkg set type=module
npm install -D typescript @types/node
  • npm pkg set type=module makes the project use modern ES modules (import/export).
  • typescript is installed per project, as a dev dependency, so everyone on the team uses the same version. Since TypeScript 7, this package contains the new native compiler; check with npx tsc --version (you should see Version 7.0.2 or later).
  • @types/node provides the types for Node’s built-in APIs such as process, fs and http.

Step 2: tsconfig.json

npx tsc --init writes a commented starter config. For the projects in this course we’ll use a smaller one. Create tsconfig.json:

JSON
{
  "compilerOptions": {
    "module": "nodenext",
    "target": "esnext",
    "types": ["node"],
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "noEmit": true,
    "allowImportingTsExtensions": true,
    "erasableSyntaxOnly": true,
    "verbatimModuleSyntax": true,
    "skipLibCheck": true
  },
  "include": ["src"]
}

What these mean, briefly (tsconfig in depth covers them properly):

Option Why
module: "nodenext" understand imports exactly the way Node does
target: "esnext" we run on a modern Node, so don’t rewrite modern syntax
types: ["node"] load Node’s types (since TypeScript 6, no @types are loaded unless listed)
strict the full set of safety checks (the default since TypeScript 6, but say it anyway)
noUncheckedIndexedAccess array[i] might be undefined — catch that
noEmit tsc only checks; Node runs the .ts files itself
allowImportingTsExtensions allow import "./util.ts", which Node needs
erasableSyntaxOnly forbid the few TypeScript features Node can’t strip (like enum)
verbatimModuleSyntax keep imports predictable for tools that strip types
skipLibCheck don’t re-check the .d.ts files of installed packages

Step 3: write and run some TypeScript

Create src/hello.ts:

TypeScript
function greet(name: string): string {
  return `Hello, ${name}!`;
}

console.log(greet("TypeScript"));

Run it:

Terminal
node src/hello.ts
Text
Hello, TypeScript!

Node removed the : string annotations and ran the JavaScript that was left. There’s no compiled file and no build step.

Step 4: type-check

Now break something on purpose — call greet(42) — and run the checker:

Terminal
npx tsc
Text
src/hello.ts(5,19): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.

The exit code is non-zero when there are errors, so scripts and CI can stop on them.

Meanwhile node src/hello.ts still runs and prints Hello, 42!, because Node doesn’t check types. This is why every TypeScript project runs tsc too.

Add scripts so nobody has to remember the commands:

Terminal
npm pkg set scripts.start="node src/hello.ts" scripts.check="tsc"
Terminal
npm start           # run
npm run check       # type-check the whole project
npx tsc --watch     # re-check on every save

Step 5: the editor

VS Code has TypeScript support built in: open the folder and type errors appear as red squiggles, hovering shows types, and F12 jumps to definitions. To use the faster TypeScript 7 language service in the editor, install the TypeScript 7 extension from the Marketplace (it ships with VS Code itself in recent releases). Other editors — WebStorm, Zed, Neovim — support TypeScript through the same language server protocol.

Useful habits from day one:

  • Hover over anything to see its type.
  • Ctrl+Space for completions; F2 to rename a symbol everywhere.
  • Read the Problems panel (Ctrl+Shift+M) — it lists every error in the project.

Other ways to run TypeScript

  • Browsers and apps: bundlers like Vite strip types as they build (see TypeScript with React).
  • tsx and Bun run .ts files with more features than Node’s type stripping (such as enum), but also don’t type-check.
  • Online: the TypeScript Playground shows errors and the JavaScript output side by side — perfect for experiments.

The two commands to remember: node file.ts runs your code; npx tsc checks it. You need both.

Exercise

Your first TypeScript project

In a new folder, set up a project exactly as in this lesson: npm init -y, set "type": "module", install typescript and @types/node as dev dependencies, and create the tsconfig.json from the lesson. Add check and start scripts. Then write src/hello.ts: a function greet(name: string, hour: number): string that returns Good morning, Good afternoon or Good evening (before 12, before 18, otherwise) followed by , <name>!. Print greetings for hours 9, 14 and 20, then a line showing the Node.js version (process.version). Run it with npm start and check it with npm run check.

TypeScript
// Terminal:
//   mkdir ts-hello && cd ts-hello
//   npm init -y
//   npm pkg set type=module
//   npm install -D typescript @types/node
//   (create tsconfig.json — see the lesson)
//   npm pkg set scripts.start="node src/hello.ts" scripts.check="tsc"

// src/hello.ts
function greet(name: string, hour: number): string {
  // ...
}

Summary

  • Install TypeScript per project: npm install -D typescript @types/node, then run it with npx tsc.
  • tsconfig.json tells the checker how strict to be, which files to check and what kind of JavaScript you target.
  • Node.js 22.18+ runs .ts files directly by stripping types — but it does not check them.
  • tsc (with noEmit) is the type checker: run it in your editor, before commits and in CI.
  • VS Code understands TypeScript out of the box; errors appear as red squiggles while you type.