Skip to content

TypeScript basics

These four tutorials cover the parts of TypeScript that every other page builds on. Each one is a hands-on page with samples from one invented online shop, OrdersPortal, compiled with TypeScript 7.0.2 and run with Node.js 22, a complete working program at the end, and a table of the real compiler errors beginners hit. Read them in order: each assumes the ones before it. You should already know basic JavaScript; if not, start with the JavaScript basics.

  • Types: annotations, inference and literal types, any, unknown and never, arrays and tuples.
  • Functions: parameter and callback types, optional and default parameters, return types including void and Promise, and generic functions.
  • Objects: object types with optional and readonly properties, interfaces, type aliases and when to use each, and classes with access modifiers.
  • Projects: ES modules and type-only imports, what tsc does to your code, and a tsconfig.json for Node.js.

You need Node.js 22.18 or later, npm and a terminal. The first tutorial shows how to install TypeScript.

  1. Types and inference: annotate values, let inference do the rest, handle untyped JSON with unknown, and type lists with arrays and tuples. You build a daily order summary.
  2. Functions and generics: type parameters and callbacks, use optional, default and options-object parameters, write return types for sync and async functions, and write generic helpers. You build a pricing module.
  3. Objects, interfaces and classes: describe objects, name them with interfaces and type aliases, and write a Cart class with private state. You build a shopping cart model.
  4. Modules and compilation: split code into modules, compile with tsc, and configure a project with tsconfig.json. You build a small multi-file project with npm scripts.

Every basics topic has its own section on one of the four pages.

Topic Section
Type annotations Types and inference: Type annotations
Type inference Types and inference: Type inference
any, unknown and never Types and inference: Any unknown and never
Arrays and tuples Types and inference: Arrays and tuples
Functions and callbacks Functions and generics: Function types
Optional and default parameters Functions and generics: Optional and default parameters
Return types Functions and generics: Return types
Generics basics Functions and generics: Generic functions
Object types Objects, interfaces and classes: Object types
Interfaces Objects, interfaces and classes: Interfaces
Types versus interfaces Objects, interfaces and classes: Types versus interfaces
Classes Objects, interfaces and classes: Classes
Modules and imports Modules and compilation: Modules and imports
Compilation with tsc Modules and compilation: Compiling with tsc
tsconfig.json Modules and compilation: Configuring tsconfig