Menu

Setting Up TypeScript

Lesson 3: Setting Up TypeScript

What do you need?

Requirement Minimum Check with
Node.js 22.18 or later (runs .ts files directly) node -v
npm Comes with Node.js npm -v
Editor VS Code or any editor with TypeScript support –

Example

Create a project:

mkdir quickcart && cd quickcart
npm init -y
npm install --save-dev typescript
npx tsc --init

This creates tsconfig.json. Add a file:

// src/index.ts
const storeName: string = 'QuickCart';
console.log(`Welcome to ${storeName}`);

Three ways to run it:

Method Command Type-checks? Best for
Compile with tsc, then run JS npx tsc then node dist/index.js Yes Libraries and production builds
Node.js type stripping node src/index.ts No Scripts, quick experiments, servers
tsx npx tsx src/index.ts No Full TypeScript syntax, watch mode

Check types without producing files:

npx tsc --noEmit

How it works

  • typescript is installed per project, so each project controls its own version.
  • Node.js 22.18 and later run .ts files by replacing type annotations with whitespace. Node does not type-check, ignores tsconfig.json, and does not support syntax that needs code generation, such as enum.
  • With Node type stripping, imports must include the extension: import { cart } from './cart.ts'.
  • A good setup: run code with Node or tsx, and run tsc --noEmit (or watch mode, tsc --noEmit --watch) to check types.

Common mistakes

Mistake Problem Fix
Installing TypeScript globally only Different projects need different versions Install it per project with --save-dev
Running tsc index.ts in a folder with tsconfig.json Error in TypeScript 6+ Run npx tsc to use the config
Expecting node file.ts to catch type errors Errors are silently ignored Run tsc --noEmit too
Using enum with Node type stripping Runtime error Use union types or as const objects (Module 6)

Key takeaways

  • Install TypeScript per project and create a tsconfig.json with tsc --init.
  • Run code with Node.js or tsx; check types with tsc --noEmit.

Try it: Set up the quickcart project, run src/index.ts with Node.js, then introduce a type error and catch it with npx tsc --noEmit.


TypeScript Tutorial

J

Jebasta





Get in Touch For Details