Whenever I call plainToClass as so:
someClass: SomeClass = plainToClass(SomeClass, somePlain)
Everything is fine. But once I change SomeClass to be abstract and rewrite the above as:
someAbstractClass: SomeAbstractClass = plainToClass(SomeAbstractClass, somePlain)
I get the error:
No overload matches this call.
Overload 1 of 2, '(cls: ClassType<BaseFeature>, plain: unknown[], options?: ClassTransformOptions): BaseFeature[]', gave the following error.
Argument of type 'typeof BaseFeature' is not assignable to parameter of type 'ClassType<BaseFeature>'.
Cannot assign an abstract constructor type to a non-abstract constructor type.
Overload 2 of 2, '(cls: ClassType<SomeAbstractClass>, plain: object, options?: ClassTransformOptions): SomeAbstractClass', gave the following error.
Argument of type 'typeof SomeAbstractClass' is not assignable to parameter of type 'ClassType<SomeAbstractClass>'
Is it not possible do use plainToClass to convert the plain to an abstractClass instance? Why not?
The problem is that a normal class and an abstract class have different signatures in TS:
export interface AbstractType<T> extends Function {
prototype: T;
}
export type Type<T> = new (...args: any[]) => T;
And due to this difference TS shows the error.
Usually there shouldn't be instances of abstract classes, only instances of classes that implemented them. Nevertheless you can hack it with any
and get desired behavior:
const someAbstractClass: SomeAbstractClass = plainToClass(
SomeAbstractClass as any, // cast to fix the error.
somePlain,
);