Setting up TypeScript
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 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
mkdir ts-hello
cd ts-hello
npm init -y
npm pkg set type=module
npm install -D typescript @types/nodenpm pkg set type=modulemakes 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 seeVersion 7.0.2or later). - @types/node provides the types for Node’s built-in APIs such as
process,fsandhttp.
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:
{
"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:
function greet(name: string): string {
return `Hello, ${name}!`;
}
console.log(greet("TypeScript"));Run it:
node src/hello.tsHello, 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:
npx tscsrc/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:
npm pkg set scripts.start="node src/hello.ts" scripts.check="tsc"npm start # run
npm run check # type-check the whole project
npx tsc --watch # re-check on every saveStep 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
.tsfiles with more features than Node’s type stripping (such asenum), 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.tsruns your code;npx tscchecks it. You need both.
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.
// 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 {
// ...
}// src/hello.ts
function greet(name: string, hour: number): string {
let part: string;
if (hour < 12) {
part = "morning";
} else if (hour < 18) {
part = "afternoon";
} else {
part = "evening";
}
return `Good ${part}, ${name}!`;
}
for (const hour of [9, 14, 20]) {
console.log(greet("Asha", hour));
}
console.log(`Running on Node.js ${process.version}`);Summary
- Install TypeScript per project:
npm install -D typescript @types/node, then run it withnpx 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
.tsfiles directly by stripping types — but it does not check them. tsc(withnoEmit) 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.