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

TypeScript's Workaround: Interface Pattern

Using interfaces with kind-encoded properties to simulate type constructors, though with significant limitations.

TypeScript's Workaround: Interface Pattern

What you'll learn: How TypeScript developers simulate higher-kinded types using interfaces with "kind-encoded" properties, and why this workaround has significant limitations.

The Core Problem Revisited

Since TypeScript lacks true higher-kinded types, you can't write a generic function that works abstractly over type constructors like Array, Promise, or custom containers. You need a workaround to encode the "shape" of a generic type.

The Interface Encoding Pattern

The most common workaround uses an interface with a marker property that encodes the type constructor's "kind":

interface HKT {
  readonly _A?: unknown;
  readonly _TYPE?: unknown;
}

interface ArrayHKT extends HKT {
  readonly _TYPE: Array<this['_A']>;
}

interface PromiseHKT extends HKT {
  readonly _TYPE: Promise<this['_A']>;
}

The _A property represents the type parameter, and _TYPE represents the resulting concrete type. You then use indexed access to "apply" the type constructor:

type Apply<F extends HKT, A> = (F & { _A: A })['_TYPE'];

type NumberArray = Apply<ArrayHKT, number>;  // Array<number>
type StringPromise = Apply<PromiseHKT, string>;  // Promise<string>

Why This Is Limited

Boilerplate explosion: Every type constructor needs its own interface definition.

No real abstraction: You're manually encoding what the type system should handle natively.

Error messages: Failures produce cryptic errors about missing properties rather than clear type constructor mismatches.

Limited composition: Nesting these patterns quickly becomes unreadable and fragile.

No type inference: TypeScript can't infer the HKT interface from actual generic types automatically.

This pattern appears in advanced libraries but remains a hack—a creative exploitation of TypeScript's structural features to approximate something the language doesn't truly support.

Key Takeaway: TypeScript's interface-based HKT workaround uses marker properties to encode type constructors, but it's verbose, brittle, and fundamentally limited compared to true higher-kinded type support in languages like Haskell or Scala.