Node.js is a runtime that lets JavaScript run outside the browser — on servers, laptops and command-line tools. TypeScript is JavaScript plus a type system: you write .ts files, and the TypeScript compiler checks your types before the code runs, catching many bugs early.
Install the current LTS (long-term support) version of Node.js from nodejs.org. It comes with npm, the package manager. Check the install with node -v and npm -v.
In a new folder, create a project with npm init -y, then install TypeScript, tsx (a fast runner for .ts files) and the Node.js type definitions as development dependencies. Then add the tsconfig.json below: it controls how the type checker works. It tells TypeScript to use the Node.js type definitions and to be strict — keep that on.
mkdir hello-ts && cd hello-ts
npm init -y
npm install --save-dev typescript tsx @types/node{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "Bundler",
"moduleDetection": "force",
"types": ["node"],
"strict": true,
"noEmit": true,
"skipLibCheck": true
}
}const name: string = "Dolese";
const year: number = new Date().getFullYear();
console.log("Hello, " + name + "! Welcome to TypeScript in " + year + ".");npx tsx hello.ts # run the file directly
npx tsc --noEmit # type-check the whole project without running itKey points
- Node.js runs JavaScript; TypeScript adds types that are checked before running.
tsxruns .ts files directly;tsctype-checks and compiles them.- Always keep
"strict": truein tsconfig.json — it catches the most bugs.
Exercise
Create about.ts that stores your name, age and school in variables and prints one sentence about you. Run it with npx tsx about.ts, then change age to a string like "twenty" while it is annotated as number and see the error tsc --noEmit gives.
Show solution
Try the exercise yourself first — then compare your approach with this one.
Store each fact in a const with a type annotation, then join them into one sentence. Running npx tsc --noEmit after changing age to a string shows TypeScript catching the mistake before the program ever runs.
const name: string = "Amina";
const age: number = 17;
const school: string = "Azania Secondary";
console.log("My name is " + name + ", I am " + age + " years old and I study at " + school + ".");npx tsx about.ts
# My name is Amina, I am 17 years old and I study at Azania Secondary.
# After changing the line to: const age: number = "twenty";
npx tsc --noEmit
# about.ts:2:7 - error TS2322: Type 'string' is not assignable to type 'number'.