Can you pass type aliases to an interface in TypeScript?

Viewed 108

I'd like to write a TypeScript interface is generic over a type function instead of just a type. In other words, I want to write something like

interface Foo<Functor> {
  bar: Functor<string>;
  baz: Functor<number>;
}

where Functor is some generic type alias I can pass in from the outside. Then I'll be able to make different kinds of Foo like this:

type Identity<T> = T;
type Maybe<T> = T | undefined;
type List<T> = T[];

// All of the following would typecheck
const fooIdentity: Foo<Identity> = { bar: "abc", baz: 42 };
const fooMaybe: Foo<Maybe> = { bar: undefined, baz: undefined };
const fooList: Foo<List> = { bar: ["abc", "def"], baz: [42, 43] };

I've tried to find a way to make the compiler accept this but no luck, so I'm wondering if there's a trick I'm missing or if TypeScript just can't express this.

0 Answers
Related