TypeScript Tutorial
JavaScript with a type system that catches mistakes early.
Getting started4 lessons
Basics9 lessons
- 5Variables: let and constPrefer const, use let when a value changes and avoid var.
- 6Type InferenceTypeScript works out types from values, so let n = 5 is a number.
- 7Numbers and Stringsnumber covers integers and floats.
- 8any, unknown and neverany switches off checking.
- 9null, undefined and Strict Null ChecksWith strictNullChecks, null and undefined are separate types.
- 10Type Assertionsas tells the compiler to treat a value as another type.
- 11TypeScript OperatorsArithmetic, comparison and logical operators work as in JavaScript.
- 12Objects and DestructuringObjects hold named properties.
- 13Typed Array Methodsmap, filter, reduce, find, some, every and flatMap keep their types, and generics let Type.
Types29 lessons
- 14Basic Typesstring, number, boolean and more.
- 15Arrays and TuplesTyped lists.
- 16Objects, Interfaces and Type AliasesDescribing shapes.
- 17Unions and Literal TypesOne of several types.
- 18EnumsNamed constants.
- 19Literal Types and const AssertionsA literal type allows only one exact value.
- 20Intersection TypesA & B combines types: a value must satisfy both.
- 21Type Aliases vs InterfacesBoth describe object shapes.
- 22Index Signatures and RecordsAn index signature describes objects with unknown keys: { [key: string]: number }.
- 23Readonly and Named Tuplesreadonly arrays and properties prevent changes.
- 24Basic TypesTypeScript adds types to JavaScript.
- 25Type InferenceTypeScript infers types from values, so you often do not need annotations.
- 26Arrays and TuplesArrays use number[] or Array<number>.
- 27any, unknown and neverany turns off checking.
- 28Union TypesA union allows several types with |.
- 29Literal TypesA literal type allows only exact values.
- 30Type Aliasestype gives a name to any type.
- 31InterfacesAn interface describes an object shape and can be extended and implemented by classes.
- 32Extending InterfacesInterfaces extend others to add properties without repeating them.
- 33readonly and Optional Propertiesreadonly stops reassignment after creation.
- 34EnumsEnums name a set of constants.
- 35Type Assertionsas tells the compiler to treat a value as a type.
- 36Type Narrowingtypeof, instanceof, in and equality checks narrow a union inside a branch.
- 37Discriminated UnionsGive each member a literal kind property.
- 38null and undefined ChecksWith strictNullChecks, null and undefined are separate types.
- 39void and nevervoid is for functions that return nothing.
- 40Index SignaturesAn index signature types an object with unknown keys.
- 41RecordRecord<K, V> builds an object type with keys K and values V.
- 42Practice: Typed Event EmitterUse a generic map of event names to payload types to make an emitter that cannot be misuse.
Control flow3 lessons
Functions and generics17 lessons
- 46FunctionsTyping parameters and returns.
- 47GenericsReusable typed code.
- 48Utility TypesPartial, Pick, Omit, Record.
- 49Optional, Default and Rest ParametersMark optional parameters with ?, give defaults with =, and collect extra arguments with ...
- 50Function Types and OverloadsA function type is (a: number) => string.
- 51this, void and CallbacksFunctions can declare the type of this.
- 52Generic Constraints and DefaultsT extends X limits what T can be.
- 53Generic Classes and InterfacesClasses and interfaces can take type parameters, such as a typed Stack or a Result type.
- 54Function TypesType parameters and the return value.
- 55Optional and Default ParametersMark a parameter optional with ? or give a default value.
- 56Rest ParametersCollect extra arguments into a typed array.
- 57Function OverloadsOverloads declare several call signatures for one implementation.
- 58GenericsGenerics make code reusable for many types while keeping safety.
- 59Generic ConstraintsUse extends to require a shape from T.
- 60Generic Interfaces and TypesInterfaces and aliases can take type parameters too.
- 61keyofkeyof gives the union of an object type's keys.
- 62typeof in Typestypeof in a type position copies the type of a value.
Classes and modules14 lessons
- 63ClassesMembers, visibility and constructors.
- 64Async, Promises and ModulesTyping promises.
- 65Class Modifierspublic, private, protected, readonly and static control access.
- 66Inheritance and Abstract Classesextends inherits from a class, super calls the parent and override marks replacements.
- 67Implementing InterfacesA class can implement several interfaces and the compiler checks every member.
- 68ES Modules and ImportsUse export and import.
- 69Declaration Files and Globals.d.ts files describe the types of JavaScript libraries.
- 70ClassesClasses have typed properties and methods and a constructor.
- 71public, private and protectedprivate limits access to the class, protected to subclasses.
- 72implementsA class can promise to follow an interface with implements.
- 73Abstract ClassesAn abstract class cannot be instantiated and may declare methods subclasses must write.
- 74Static and Accessorsstatic belongs to the class.
- 75Modules: import and exportFiles with import or export are modules.
- 76Decorators OverviewDecorators attach behaviour to classes and methods.
Advanced typesPremium5 lessons
- 77Mapped TypesA mapped type builds a new type by transforming each key: { [K in keyof T]: .
- 78Conditional Types and inferT extends U ? X : Y chooses a type based on a condition.
- 79Template Literal TypesTemplate literal types build string types from other types, such as "on" + Capitalize<Even.
- 80More Utility TypesExclude, Extract, NonNullable, ReturnType, Parameters, Awaited, InstanceType and Required .
- 81Decorators and SymbolsDecorators annotate classes and members, and are used by Angular and NestJS.
Async and toolingPremium6 lessons
- 82Promises and async/await Typesasync functions return Promise<T>.
- 83Error Handling and Result TypesCaught errors are unknown under strict mode, so narrow them.
- 84JSON and Runtime ValidationTypes vanish at runtime, so data from the network needs validation.
- 85TypeScript with the DOMQuery results are nullable, so check them.
- 86Testing TypeScriptUse Vitest or Jest with TypeScript.
- 87Linting, Formatting and Build ToolsESLint with typescript-eslint finds bugs, Prettier formats, and bundlers such as Vite, esb.
TypeScript in practicePremium5 lessons
- 88Typed Express APIsAdd @types/express for typed requests and responses.
- 89TypeScript with ReactType component props with an interface, hooks with generics such as useState<number>, and .
- 90Project References and MonoreposProject references split large codebases into smaller TypeScript projects that build incre.
- 91Migrating JavaScript to TypeScriptRename files gradually from .js to .ts, enable allowJs and checkJs, start with strict off .
- 92TypeScript Best PracticesEnable strict, avoid any, prefer unknown, use unions and discriminated unions instead of e.
More types9 lessons
- 93Partial, Pick and OmitPartial makes all properties optional, Pick keeps some and Omit removes some.
- 94Readonly and RequiredReadonly freezes properties in the type.
- 95Mapped TypesA mapped type builds a new type by looping over keys.
- 96Conditional TypesT extends U ? X : Y picks a type by a condition.
- 97Template Literal TypesBuild string types from others with template syntax.
- 98as constas const makes values readonly literals, great for lookup tables and tuples.
- 99Custom Type GuardsA function returning x is T tells the compiler a check narrows the type.
- 100satisfiessatisfies checks a value against a type without widening it.
- 101Inferring with inferinfer captures a type inside a conditional type.
Tooling basics13 lessons
- 102Promises and async TypesAsync functions return Promise<T>.
- 103Typing ErrorsCaught errors are unknown.
- 104tsconfig.jsontsconfig.json sets compiler options.
- 105Useful Strict FlagsnoImplicitAny, strictNullChecks, noUncheckedIndexedAccess and exactOptionalPropertyTypes c.
- 106Using tscnpx tsc compiles, tsc --noEmit only checks and tsc --watch rebuilds on change.
- 107Using JavaScript LibrariesInstall @types/pkg for libraries without types.
- 108Generic ClassesClasses can take type parameters for containers.
- 109Immutable UpdatesUse readonly arrays and spread to update without mutating.
- 110Object Types and Excess PropertiesObject literals are checked for extra properties.
- 111Exhaustive ChecksUse never in the default branch so a new union member causes a compile error.
- 112Practice: Typed Todo ListModel data with an interface, then write small typed functions.
- 113Practice: Generic CacheCombine Map and generics for a typed cache.
- 114Practice: API Result TypeModel success and failure with a discriminated union.