Issue
As I work on my project, I encountered an error while trying to create a decorator for a class:
Error: Type 'typeof Controller' is not assignable to type 'typeof MainController'.
Cannot assign an abstract constructor type to a non-abstract constructor type.
My Implementation
Let me share the code snippet that triggered the error:
File 1
export function customDecorator(argument: string) {
return (classType: typeof Base) => {
// additional code
return classType;
};
}
export function anotherDecorator(argument: string) {
return (classType: Base, ...) => {
// more code
};
}
export abstract class Base {
// implementing some methods
}
File 2
import { customDecorator, anotherDecorator, Base } from "./file1";
@customDecorator("some text") // this line triggers the error
class Derived extends Base {
@anotherDecorator("other text") // no issues here
public exampleMethod() {}
}
I'm uncertain about what could be causing this problem. It seems like it should work fine as per usual. Do you have any suggestions or insights? My goal is simply to limit the myDecorator
functionality to classes derived from Base
.
Update: I was able to resolve the issue. See below for details.