Day 1 – TypeScript Setup and Type System
Today's Focus
Set up a TypeScript project from scratch and understand the compiler, tsconfig.json, and the type system fundamentals.
Tasks
- Initialise a Node.js project with
npm init -y, then install TypeScript:npm install --save-dev typescript. Runnpx tsc --initand opentsconfig.json. Enable"strict": trueand set"outDir": "dist"and"rootDir": "src". - Write a
src/index.tsfile with variables of primitive types (string,number,boolean,null,undefined). Deliberately introduce a type error (assign a string to a number variable) and runnpx tsc --noEmitto see the error. Fix it. - Add
npm run buildandnpm run typecheckscripts topackage.json. Confirmbuildcompiles todist/andtypecheckcatches errors without emitting. - Explore the difference between
any,unknown, andnever: write a function that usesunknownas a parameter type and requires a type guard (typeof x === "string") before using it. Compare to usinganyand explain whyunknownis safer. - Define a union type (
type Status = "pending" | "active" | "archived") and an intersection type (type AdminUser = User & { role: "admin" }). Write a function for each that is fully type-safe. - Convert a plain JavaScript file (any small utility you wrote in Week 1 or 2) to TypeScript by adding type annotations until
tsc --noEmitpasses with no errors.
Reading / Reference
- TypeScript Handbook — The Basics, Everyday Types, and Narrowing.
- tsconfig.json reference — focus on
strict,target,module,outDir,rootDir. - TypeScript Deep Dive by Basarat — Chapters on Getting Started and Type System.