TypeScript
JavaScript with static types, checked before the code runs: type annotations and inference, objects and interfaces, unions and narrowing, generics and the type-level tools behind them, and how to use TypeScript in real projects, from typing API data to migrating an existing JavaScript code base.
18 pages 97 glossary terms
How to use this topic
This topic builds on JavaScript: TypeScript adds types to the language, so the pages explain the types and link to the JavaScript pages for everything else. The first eight pages cover the everyday type system: what TypeScript is, setting it up, basic types, functions, objects and interfaces, unions, narrowing and null safety. The next six go deeper: classes, generics, enums, and the type operators, utility types and mapped and conditional types. The last four are about real projects: modules and declaration files, typing API data, running TypeScript in Node.js and the browser, and tooling and migration.
Every example is type-checked with the TypeScript compiler in strict mode and then run with Node.js, and shows its exact output. Examples that show a type error show the compiler's message instead.
Learning path
- 01What Is TypeScript?What TypeScript is and why teams use it: JavaScript with static types that are checked before the code runs and removed when it is compiled, where it is used, and the new native compiler.
- 02Setting Up: tsc, tsconfig.json and Running TypeScriptInstalling TypeScript, compiling with tsc, configuring a project with tsconfig.json and strict mode, and the ways to run TypeScript code in Node.js.
- 03Basic Types and Type AnnotationsAnnotating variables with string, number, boolean, arrays and tuples, letting the compiler infer types, and the special types any, unknown and never.
- 04FunctionsTyping functions in TypeScript: parameter and return types, optional and default parameters, rest parameters, function types, void, and overloads.
- 05Objects, Interfaces and Type AliasesDescribing the shape of objects: object types, interfaces and type aliases, optional and readonly properties, index signatures, and structural typing.
- 06Union, Intersection and Literal TypesCombining types: unions for values that can be one of several types, literal types for exact values, and intersections that merge object types.
- 07Narrowing and Type GuardsHow TypeScript narrows a union to one type: typeof, instanceof and in checks, discriminated unions, exhaustiveness checks with never, and custom type guards.
- 08null, undefined and Strict Null ChecksHow strictNullChecks catches the most common JavaScript error, optional properties and chaining, nullish coalescing, and why the non-null assertion is a last resort.
- 09ClassesClasses in TypeScript: typed fields and constructors, public, private and protected, parameter properties, readonly, abstract classes, implementing interfaces, and #private fields.
- 10GenericsWriting reusable, type-safe code with generics: generic functions, interfaces and classes, constraints with extends, default type parameters and inference.
- 11Enums, const Assertions and satisfiesRepresenting fixed sets of values: TypeScript enums and their pitfalls, union-of-literals alternatives, as const, and the satisfies operator.
- 12Type Operators: keyof, typeof and Indexed AccessBuilding types from other types and values: keyof, the typeof type operator, indexed access types and lookups into arrays and objects.
- 13Utility TypesThe built-in utility types and when to use them: Partial, Required, Readonly, Pick, Omit, Record, Exclude, Extract, NonNullable, ReturnType, Parameters and Awaited.
- 14Mapped, Conditional and Template Literal TypesAdvanced type-level tools: mapped types, conditional types with infer, template literal types, and how the built-in utility types are made from them.
- 15Modules, Declaration Files and Typing JavaScript LibrariesModules in TypeScript, import type, declaration files (.d.ts), types from DefinitelyTyped (@types), typing JavaScript with JSDoc, and declaring your own globals.
- 16Async Code and Typing API DataTyping promises and async functions, why data from JSON and APIs is unknown until checked, validating it at runtime, and handling errors in catch.
- 17TypeScript in Node.js and the BrowserRunning and building TypeScript in practice: Node.js type stripping and tsx, bundlers for the browser, DOM types, and source maps for debugging.
- 18Tooling, Configuration and Migrating from JavaScriptThe tsconfig.json options that matter, linting with typescript-eslint, checking JavaScript with checkJs, and moving an existing JavaScript project to TypeScript step by step.
TypeScript glossary
Short definitions of the terms used in this topic. The full glossary covers every topic.