Abstract class constructor type issue
See original GitHub issueI 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:
- Created 5 years ago
- Comments:9
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
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
I tried registering the abstract class using
@autoInjectable
in my PR #21 which worked fine except for a type error onreturn class extends target
because I changed theconstructor
type. Error message:Type constructor<any> is not a constructor function type
https://travis-ci.org/Microsoft/tsyringe/jobs/477314771I 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