Abstract constructor type in TypeScript

John Weisz picture John Weisz · Apr 27, 2016 · Viewed 18.8k times · Source

The type signature for a non-abstract class (non-abstract constructor function) in TypeScript is the following:

declare type ConstructorFunction = new (...args: any[]) => any;

This is also called a newable type. However, I need a type signature for an abstract class (abstract constructor function). I understand it can be defined as having the type Function, but that is way too broad. Isn't there a more precise alternative?


Edit:

To clarify what I mean, the following little snippet demonstrates the difference between an abstract constructor and a non-abstract constructor:

declare type ConstructorFunction = new (...args: any[]) => any;

abstract class Utilities {
    ...
}

var UtilityClass: ConstructorFunction = Utilities; // Error.

Type 'typeof Utilities' is not assignable to type 'new (...args: any[]) => any'.

Cannot assign an abstract constructor type to a non-abstract constructor type.

Answer

Tehau Cave picture Tehau Cave · Jul 28, 2016

Was just struggling with a similar problem myself, and this seems to work for me:

type Constructor<T> = Function & { prototype: T }