TypeScript Fundamentals
Add static types to JavaScript so the editor catches mistakes before your users do, in both React and Node.js.
Intermediate · about 10 hours · difficulty 3/5
Learn first: JavaScript Fundamentals, npm, Packages and Build Tooling
TypeScript is the default for serious full-stack work. Sharing types between frontend and backend means a renamed field breaks the build, not production.
Basics
- Annotate function parameters and return types; let inference handle most variables.
- Primitive types, arrays, tuples, and literal types like "PENDING" | "PAID".
- type and interface to describe objects. Either works; be consistent.
- Optional properties (?) and union types (string | null).
Narrowing
TypeScript follows your checks. After if (user === null) return, it knows user is defined. Use typeof, in, and discriminated unions (a shared status field) to model states like loading, success and error.
Configuration
tsconfig.json controls strictness. Turn on strict from day one; adding it later is painful. Learn what target, module and paths do.
Using it in practice
Type your API responses, your React props and your database models. Libraries like Prisma and Zod generate types for you, so you rarely write them twice.
Pitfalls
- Sprinkling any to silence errors. Prefer unknown and narrow it.
- Using as type assertions to lie to the compiler.
- Thinking types exist at runtime. They are erased; incoming data still needs validation.
You are ready to move on when you can convert a small JavaScript project to strict TypeScript with no any and explain each error you fixed.
Key concepts
- Type annotations and inference
- Object types: type vs interface
- Union and literal types
- Narrowing and type guards
- Discriminated unions
- tsconfig and strict mode
- any vs unknown
- Typing third-party libraries
Mini project
Convert your placement offers script to strict TypeScript. Model an Offer type with a status union (APPLIED, SHORTLISTED, OFFERED, REJECTED) and write typed functions to filter and summarise offers.
Check yourself
If TypeScript types are erased at runtime, what still protects your API from a client sending a string where you expect a number?
Free resources
- The TypeScript Handbook (TypeScript)
- TypeScript Roadmap (roadmap.sh)
- Total TypeScript free tutorials (Total TypeScript)