MichalLytek / class-transformer-validator

A simple plugin for class-transformer and class-validator which combines them in a nice and programmer-friendly API.
MIT License
200 stars 19 forks source link

Allow check for abstract class #32

Open wwwzbwcom opened 3 years ago

wwwzbwcom commented 3 years ago

Hello, currently abstract class cant pass transformAndValidate type check.

When export declare type ClassType<T> = new (...args: any[]) => T;, abstract class validate can't pass type check.

abstract class User {
    @IsString()
    @MinLength(5)
    name: string;
}

let user: User = {
    name: 'hello'
};

transformAndValidate(User, user);

Use export type ClassType<T> = {prototype: T}; to allow check for abstract class, and plain object also cant pass check

MichalLytek commented 3 years ago

@wwwzbwcom It solves the args problem but makes an issue for the receiver - you can't do new Foo() anymore as without new signature the type with prototype is not newable.

And I think TypeScript do a good job here - abstract class in abstract, you shouldn't be able to create an instance of such class. transformAndValidate does exactly that, returning you an instance of abstract User class.

I think you should change the way you define your abstract and non-abstract classes.