Course contentsShow
← TypeScript
Lesson 1502 of 1,79634. The Limits of TypeScriptFree lesson

Libraries That Use HKT Simulation: fp-ts

Exploring how fp-ts leverages these patterns for functional programming abstractions in TypeScript.

Libraries That Use HKT Simulation: fp-ts

You'll learn how the fp-ts library leverages higher-kinded type simulation to provide functional programming abstractions like Option, Either, and Array with a consistent generic interface.

What fp-ts Does with HKT Simulation

fp-ts is a functional programming library for TypeScript that brings concepts from languages like Haskell and Scala. Since TypeScript doesn't have true higher-kinded types, fp-ts uses the interface-with-type pattern and URI registry you've just learned to create polymorphic abstractions.

This means you can write a single map function that works over Option<A>, Either<E, A>, Array<A>, and any other "container" type—just like you would in a language with real HKTs.

How fp-ts Structures Its HKT System

fp-ts defines:

  • URIs: String literal types like 'Option', 'Either', 'Array'
  • A global URItoKind2 interface: Maps each URI to its concrete type constructor
  • Typeclasses: Interfaces like Functor, Monad, and Applicative that declare operations generically
  • Instances: Concrete implementations per type (e.g., Option has a Functor instance)

For example, the Functor typeclass defines a map operation. The Option type registers itself in the URI system, and fp-ts provides a Functor<'Option'> instance. Now you can use map on Option values through this abstraction layer.

The Benefit and the Cost

Benefit: Write generic, reusable functions once. For instance, a function that works on "any Functor" automatically supports Option, Either, Array, etc.

Cost: Steep learning curve, verbose type annotations, and complex error messages. fp-ts trades ergonomics for powerful abstraction—ideal for teams committed to functional programming, but overkill for most TypeScript codebases.

Key Takeaway: fp-ts demonstrates the most advanced use of HKT simulation in TypeScript, enabling functional programming abstractions at the cost of significant complexity—use it when algebraic abstractions justify the trade-off.