question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Abstract class constructor type issue

See original GitHub issue

I tried using the injectable decorator with an abstract class but it gave me this error

Argument of type 'typeof MyClass' is not assignable to parameter of type 'constructor<{}>'.
      Cannot assign an abstract constructor type to a non-abstract constructor type.

Sample code

import { injectable } from "tsyringe";

@injectable()
abstract class MyClass {

}

I modified the declaration just to test (based on https://stackoverflow.com/questions/36886082/abstract-constructor-type-in-typescript)

type abstract_ctor<T> = Function & { prototype: T };
export declare function injectable<T>(): (target: constructor<T>|abstract_ctor<T>) => void;

and the compilation worked without errors

I will try cloning and running some tests to see if DI works fine with an abstract class

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
unlocomqxcommented, Jan 12, 2019

Thank you for the hint, I didn’t think it would work without the base class decorator (coming from inversify) Your example works, but the following doesn’t

test("abstract class autoinjectable", () => {
  class Foo {}
  abstract class MyBase {
    constructor(public foo: Foo){}
  }

  @injectable()
  class MyClass extends MyBase {
  }

  const instance: MyClass = globalContainer.resolve(MyClass);
  expect(instance.foo instanceof Foo).toBeTruthy();
});

Do you think it can be implemented? Is the concept correct at least?

If not I can just duplicate the constructor in my child classes

Thank you

1reaction
unlocomqxcommented, Jan 10, 2019

I tried registering the abstract class using @autoInjectable in my PR #21 which worked fine except for a type error on return class extends target because I changed the constructor type. Error message: Type constructor<any> is not a constructor function type https://travis-ci.org/Microsoft/tsyringe/jobs/477314771

I didn’t find another way since the base constructor must be altered

But anyway, the base class seems to be present in the metadata So we may take the base constructor params, instantiate them and assign them as props of the derived class, I don’t know if it can be done or if it’s a clean solution 2019-01-10_13-46

Read more comments on GitHub >

github_iconTop Results From Across the Web

Abstract constructor type in TypeScript - Stack Overflow
I've edited a minimal reproducible example into the question to demonstrate the problem. Basically, I need to pass a class as an argument....
Read more >
TypeScript, abstract classes, and constructors - LogRocket Blog
TypeScript abstract classes cannot be instantiated directly; only nonabstract subclasses can be. What does this mean for constructor usage?
Read more >
Constructors in Java Abstract Classes - Baeldung
A constructor is a method called when a class is instantiated, and an abstract class cannot be instantiated. It sounds counterintuitive, right?
Read more >
Why Java Interfaces Cannot Have Constructor But Abstract ...
The constructor inside the abstract class can only be called during constructor chaining i.e. when we create an instance of sub-classes. This is ......
Read more >
Playground Example - Abstract Class Constructors - TypeScript
TypeScript has supported abstract classes since 2015, which provides compiler errors if you try to instantiate that class. TypeScript 4.2 adds support for ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found