Search code examples
typescripttypescript-generics

Typescript: generic that extends a type with a generic (higher kinded types)


Say I have an interface:

interface Applicative<T> {}

Now I want to define a function f that:

  • takes a function and takes a type U extending an Applicative wrapping Any
  • and returns a U wrapping a function

How do I define this in Typescript? I tried the following but it's not valid:

function f<U extends Applicative>(fn: Function, a: U<any>): U<Function>

Solution

  • Unfortunately, typescript does not yet implement higher kinded types.

    See microsoft/typescript#1213 - Allow classes to be parametric in other parametric classes for more details.