BeginnerTypeScript · Lesson 1 of 7

Setting up Node.js & TypeScript

Install the tools, create a project and run your first program.

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.

TerminalShell
mkdir hello-ts && cd hello-ts
npm init -y
npm install --save-dev typescript tsx @types/node
tsconfig.jsonJSON
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "moduleDetection": "force",
    "types": ["node"],
    "strict": true,
    "noEmit": true,
    "skipLibCheck": true
  }
}
hello.tsTypeScript
const name: string = "Dolese";
const year: number = new Date().getFullYear();

console.log("Hello, " + name + "! Welcome to TypeScript in " + year + ".");
TerminalShell
npx tsx hello.ts      # run the file directly
npx tsc --noEmit      # type-check the whole project without running it

Key points

  • Node.js runs JavaScript; TypeScript adds types that are checked before running.
  • tsx runs .ts files directly; tsc type-checks and compiles them.
  • Always keep "strict": true in 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.

about.tsTypeScript
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 + ".");
TerminalShell
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'.

Check your understanding

  1. What does TypeScript add on top of JavaScript?

  2. Which command runs a .ts file directly during development?

  3. What does npx tsc --noEmit do?

  4. Why keep "strict": true in tsconfig.json?

Ask AI