Magician
Magician

Reputation: 2113

Typescript: instance of an abstract class

I have an abstract class Router:

export abstract class Router {
}

And an interface like this

interface IModule {
    name: string;
    forms: Array<IForm>,
    route: typeof Router
}

Now I have a class which looks like this, and many others based on Router abstract

export class Module1 extends Router {
}

Now, I want to instantiate the route like this:

let module: IModule = { 
    name: "My Module",
    forms: [],
    route: Module1
}

let router = new module.route(); 
// TS2511: Cannot create an instance of an abstract class.

The code run just fine, and the instance of router = new Module1() is made properly, but I obviously doesn't write it properly in Typescript because I see TS2511: Cannot create an instance of an abstract class. when transpiling.

What is the correct way to define this?

Upvotes: 19

Views: 22986

Answers (4)

goose_lake
goose_lake

Reputation: 1438

In short, the type-safe and non-duplicating way to define type for a non-abstract contructor of the same signature as given abstract class is new (...args: ConstructorParameters<typeof AbstractClass>) => AbstractClass;. See TS docs. So for case in this question, it would be:

let module: IModule = { 
    name: "My Module",
    forms: [],
    route: new (...args: ConstructorParameters<typeof Router>) => Router
}

Longer explanation:

What existing answers don't mention is the case when a constructor of the base class has a non-empty signature, i.e.

abstract class Router {
  constructor(name: string) {
  }
}
export class Module1 extends Router {
}

, creating a type with new () => Router won't work, as the new () function needs params in the definition. As suggested in a comment, it's possible to use new (...args: any[]) => Router, but that erases the type-safety of constructor, and allows calling it with any arbitrary arguments, potentially leading to issues. Defining the new () signature manually, like so new (name: string) => Router, leads to code duplication, as you're defining the same signature in two places.

That's why the ConstructorParameters<T> utility type is needed.

Upvotes: 0

Parzh from Ukraine
Parzh from Ukraine

Reputation: 9873

Your intention is to say that .route property of IModule is any class (a.k.a constructor, a.k.a new-able function) that returns an instance derived from Router, while the following definition:

interface IModule {
    route: typeof Router
}

… basically says that .route is the Router class itself (this is not exactly true but it is fine for our intents and purposes). Since Router class is abstract, and abstract classes cannot be instantiated, you get an error:

new module.route()
// Error: Cannot create an instance of an abstract class

You need to fix the definition from "the Router constructor itself" to "any class that returns an instance derived from Router". You can do it in two ways.

If you do it loosely (similar to setting the type to any or unknown), you get the basic functionality you need, but you can't use methods and properties of the actual constructor you provide there, because you don't give this information to TypeScript:

interface IModule {
    route: new () => Router
}

const module: IModule = {
    route: Module1,
}
 
new module.route()

Try it

Alternatively, you can do it strictly (similar to specifying an exact type of a variable), which would mean creating a type argument, which would mean defining IModule as a generic:

interface IModule<R extends new () => Router> {
    route: R
}

const module: IModule<typeof Module1> = { 
    route: Module1
}

new module.route()

Try it

Upvotes: 1

spinkus
spinkus

Reputation: 8550

Note, second part of the accepted answer covers:

if you need to access static methods of Router ...

The proposed solution doesn't actually work. Just spent fair bit of time trying to figure this out so though I'd add: the way to do this is:

type RouterDerived = {new (): Router} & typeof Router;

Full example playground.

Upvotes: 10

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 249656

When you have an abstract class, it's constructor is only invokable from a derived class, since the class is abstract and should not be instantiated. This carries over when you type something as typeof AbstractClass, the constructor will not be callable.

The solution would to be to not use typeof Router but rather a constructor signature that returns a Router

interface IModule {
    name: string;
    forms: Array<IForm>,
    route: new () => Router
}

Another solution if you need to access static methods of Router is to create an interface derived from typeof Router which will erase the abstractness of the constructor:

export abstract class Router {
    static m(): void { console.log("Router"); }
}
type RouterClass = typeof Router;
interface RouterDerived extends RouterClass { }

interface IModule {
    name: string;
    route: RouterDerived
}

export class Module1 extends Router {
    static m(): void { console.log("Module1"); }
}

let module: IModule = { 
    name: "My Module",
    route: Module1
}

module.route.m();
let router = new module.route(); 

Upvotes: 26

Related Questions