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.
What this section covers
Section titled βWhat this section coversβ- Types: annotations, inference and literal types,
any,unknownandnever, arrays and tuples. - Functions: parameter and callback types, optional and default parameters, return types including
voidandPromise, and generic functions. - Objects: object types with optional and
readonlyproperties, interfaces, type aliases and when to use each, and classes with access modifiers. - Projects: ES modules and type-only imports, what
tscdoes to your code, and atsconfig.jsonfor Node.js.
You need Node.js 22.18 or later, npm and a terminal. The first tutorial shows how to install TypeScript.
Learning path
Section titled βLearning pathβ- 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. - 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.
- Objects, interfaces and classes: describe objects, name them with interfaces and type aliases, and write a
Cartclass with private state. You build a shopping cart model. - Modules and compilation: split code into modules, compile with
tsc, and configure a project withtsconfig.json. You build a small multi-file project with npm scripts.
Map of topics
Section titled βMap of topicsβ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 |
Where to go next
Section titled βWhere to go nextβ- TypeScript topic hubs: eight hubs that cover every TypeScript feature in depth, in a suggested order.
- TypeScript features by topic: a one-page index of features and the version that introduced each.
- TypeScript interview questions