The URI Registry Pattern
What you'll learn: How to use string literal types as unique identifiers to register and look up type constructors in a global registry for higher-kinded type simulation.
When simulating higher-kinded types in TypeScript, you need a way to connect a "type constructor name" (like Array or Promise) with its actual type-level representation. The URI Registry Pattern solves this by using string literal types as unique keys in a global interface.
How It Works
Think of the URI registry like a phonebook for type constructors. Each "higher-kinded type" gets a unique string identifier (its URI), and that string maps to the actual type shape in a centralized registry interface.
Here's the pattern:
// The global registry interface
interface URItoKind<A> {
Array: Array<A>
Promise: Promise<A>
// Add more type constructors here
}
// Extract the actual type using the URI
type Kind<URI extends keyof URItoKind<any>, A> = URItoKind<A>[URI]
Now you can write generic code that works with any registered type constructor:
type ApplyArray = Kind<'Array', number> // Array<number>
type ApplyPromise = Kind<'Promise', string> // Promise<string>
Why String Literals?
String literals act as nominal identifiers in TypeScript's structural type system. 'Array' and 'Promise' are distinct, unique types that can't accidentally collide. The registry maps these strings to their corresponding generic types, allowing type-level "lookup" operations.
This pattern is the foundation for libraries that need to abstract over different container types while maintaining full type safety—without true higher-kinded type support.
Key Takeaway: The URI Registry Pattern uses string literal types as unique keys in a global interface to map type constructor names to their actual generic types, enabling type-safe lookups in HKT simulation.