0Pricing
TypeScript Academy · Lesson

Defining Type Constructors

Encode type constructors as interface lookups.

Naming Type Constructors

The encoding starts by giving each type constructor a unique string identifier called a URI. The URI is a tag that stands in for the constructor wherever we cannot pass the constructor itself.

type ArrayURI = "Array";
type OptionURI = "Option";
// Each container gets a unique string tag

A Registry Interface

We keep a single interface that maps each URI to the concrete type it produces for a given argument A. This interface is the registry. Each key is a URI; each value is the resolved type.

interface URItoKind<A> {
  Array: Array<A>;
  Option: A | null;
}
// URItoKind<number>["Array"] is number[]

All lessons in this course

  1. The HKT Problem in TypeScript
  2. Defining Type Constructors
  3. The Lightweight HKT Pattern
  4. Generic Functors and Mappers
← Back to TypeScript Academy