0Pricing
TypeScript Academy · Lesson

The Lightweight HKT Pattern

Implement the Kind/URI defunctionalization trick.

Defunctionalization

The trick behind the encoding has a name: defunctionalization. Instead of a real higher-order type function, we represent each function by a tag and resolve it with a single first-order lookup. The URItoKind registry is that lookup table.

type Kind<F extends URIS, A> = URItoKind<A>[F];
// One lookup replaces true type-function application

The Kind Type

Kind<F, A> is the public face of the pattern. Read it as "the constructor F applied to A". It hides the registry indexing behind a clean name.

type A = Kind<"Array", number>;  // number[]
type B = Kind<"Option", string>; // string | null

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