Aaron Aaron - 9 months ago 43
TypeScript Question

Using a generic type argument with `typeof T`

I have a factory-like function that is meant to return an sub-class instance of

. It currently looks something like this (trimmed out irrelevant parts):

function getApi<T extends BaseApi>(apiClass: typeof BaseApi): T {
return new apiClass();

And I use it like this:

const someApi = getApi<SomeApi>(SomeApi);

This works, but I would like
to be inferred by virtue of the fact that I'm passing the
constructor in. If I omit
is inferred to be of type
, not
. Worse, there's really no compiler correlation between
<T extends BaseApi>
typeof BaseApi
being the same thing, so you can incorrectly do something like
without a compiler error.

So I tried defining the
typeof T

function getApi<T extends BaseApi>(apiClass: typeof T): T {
return new apiClass();

And I found that TS did not understand this usage of
. Is there any way to do this?

Answer Source

What you actually want is new() => T, since you intend to use the argument as a constructor and produce a T. Even if you could write typeof T, that wouldn't be what you want, since T might not have a zero-argument constructor.

Remember that the typeof operator takes a value and produces a value. T is already a type; it is not a value.

Naturally, this is addressed in the TypeScript FAQ https://github.com/Microsoft/TypeScript/wiki/FAQ#why-cant-i-write-typeof-t-new-t-or-instanceof-t-in-my-generic-function