InteractiveFrameworks

TypeScript

The TypeScript that NestJS and Hono are written in, from types that describe data to the decorators and metadata that make dependency injection work. Every lesson is graded by tests and by the compiler itself: a type error blocks Run the way a build would, so some of the checks are types that have to be exactly right.

lessons
8
level
beginner
time
3.5 hours
Start with Types and narrowing

Describing data

Types for the values a program handles, and the checks that turn an unknown value into a known one.

  1. 1
    Types and narrowing

    Describe data with types precise enough to rule out wrong values, then turn a value of unknown shape, such as a request body, into one TypeScript trusts, one check at a time.

    Read the theory
  2. 2
    Interfaces

    Describe the shape of an object: required, optional and readonly properties, one interface extending another, methods an object may have, and records with fixed keys. Then see that TypeScript compares shapes, not names.

    Read the theory
  3. 3
    Generics

    Write code once for every type it can hold, without giving up the checks: generic functions whose output type follows their input, keyof and indexed access types, and a generic repository constrained to things with an id, the shape of every repository in NestJS.

    Read the theory
  4. 4
    Utility types

    Derive types from types instead of writing them twice: Omit for what a client sends to create, Partial for what it sends to update, Pick for a summary, and a mapped type of your own. The same idea as NestJS's PartialType and OmitType, which apply these to DTO classes.

    Read the theory

Classes and async code

Objects that keep state and receive their dependencies, and code that waits.

  1. 5
    Classes

    A service that receives its store through its constructor and hides what it keeps, the shape of every provider in NestJS: parameter properties, private and readonly, static members, getters, and a class that implements an interface.

    Read the theory
  2. 6
    Async code

    A server waits on everything: databases, other services, timers. Run independent work at the same time, collect results whether each succeeded or not, give up after a deadline, and try again after a failure, with types that say what each promise carries.

    Read the theory

Decorators and metadata

What @Controller, @Get and @Injectable are made of, and how a framework builds objects from type information.

  1. 7
    Decorators

    @Controller('cats') and @Get(':id') are ordinary functions that run when a class is defined. Write your own: a class decorator and method decorators that record routes, and one that wraps a method to log its calls.

    Read the theory
  2. 8
    Metadata and injection

    How NestJS knows what to pass to your constructor: the compiler records each decorated class's parameter types as metadata, and a container reads them to build every class with its dependencies. Build that container.

    Read the theory