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

Languages That Have HKTs

Comparing TypeScript to Haskell, Scala, and other languages where HKTs enable powerful abstractions like Functor.

Languages That Have HKTs

What you'll learn: How TypeScript compares to languages like Haskell and Scala that natively support higher-kinded types, and what becomes possible with true HKT support.

The HKT Hall of Fame

Several languages provide native support for higher-kinded types as a first-class language feature. Understanding what they enable helps clarify both TypeScript's limitations and the workarounds we employ.

Haskell: The HKT Pioneer

Haskell treats type constructors as values that can be abstracted over. You can write a single map function that works for Maybe, List, Tree, or any structure that implements the Functor type class.

In Haskell, Functor is defined generically over any type constructor f:

class Functor f where
  fmap :: (a -> b) -> f a -> f b

This says "for any type constructor f, provide a way to map a function over it." The same interface works whether f is Maybe, [] (list), or a custom data type.

Scala: HKTs in Object-Oriented Context

Scala brings HKTs to a more familiar object-oriented setting using type parameters with kinds. Libraries like Cats use this to create powerful abstractions:

trait Functor[F[_]] {
  def map[A, B](fa: F[A])(f: A => B): F[B]
}

The F[_] syntax means "a type constructor that takes one type parameter" — higher-kinded.

Other Languages with HKTs

  • OCaml supports HKTs through functors (parameterized modules)
  • Rust is exploring HKTs through Generic Associated Types (GATs)
  • PureScript has full HKT support similar to Haskell

What HKTs Enable: Functor as Example

The Functor abstraction is impossible to express directly in TypeScript because it requires abstracting over the container type itself (Maybe, Array, Either) rather than just the contained value.

With HKTs, you write one generic sequence function that works for all Functors. Without them, you write sequenceMaybe, sequenceArray, sequenceEither — separate implementations for each container.

Key Takeaway: Haskell, Scala, and similar languages support true higher-kinded types, enabling powerful abstractions like Functor that apply uniformly across different container types — something TypeScript's type system cannot directly express without encoding tricks or code duplication.