Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

TS
TypeScript
0/10 completed
Beginner
Why TypeScript?Basic types and inferenceTyping functionsObjects, interfaces and type aliases
Intermediate
Unions and narrowingLiteral types, tuples and enumsGenerics
Advanced
Utility typesMapped and conditional types
Pro
TypeScript in real projects
Tracks / TypeScript / Intermediate

Generics

Generics let you write code that works with many types while keeping full type safety. They're like function parameters, but for types. Arrays (Array<T>), Promises (Promise<T>), React's useState<T> and nearly every library use them.

The problem

TypeScript · editable

A generic function

<T> declares a type parameter. TypeScript fills it in from the arguments:

TypeScript · editable

The type flows in through the argument and out through the return type.

Multiple type parameters

TypeScript · editable

Constraints: extends

Sometimes the generic type must have certain features. Use extends to require them:

TypeScript · editable

keyof: safe property access

TypeScript · editable

keyof T is the union of T's property names, and T[K] is the type of that property. Together they give you fully type-safe dynamic access.

Generic types and interfaces

TypeScript · editable

One ApiResponse definition works for every endpoint.

Generic classes

TypeScript · editable

Default type parameters

TypeScript · editable

Practice

  1. Write last<T>(items: T[]): T | undefined.
  2. Write groupBy<T, K extends string>(items: T[], getKey: (item: T) => K): Record<K, T[]>.
  3. Use it to group these orders by status, and check that the result is fully typed.
TypeScript · editable
✎ Check your understanding

1.What does <T> in function wrap<T>(value: T) mean?

2.What does K extends keyof T guarantee?

← Literal types, tuples and enums