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

Typing for `transformAndValidate` with array is not correct #27

Closed coler-j closed 4 years ago

coler-j commented 4 years ago

The types for transformAndValidate is declared as: export declare function transformAndValidate<T extends object>(classType: ClassType<T>, object: object, options?: TransformValidationOptions): Promise<T>;

Where the return is typed as <T>

However, if the return of transformAndValidate is an array of transformed instances then you need t do something like this to get the typing correct:

as unknown as InstanceClass[]

MichalLytek commented 4 years ago

transformAndValidate has overloads and has a one for array:

https://github.com/MichalLytek/class-transformer-validator/blob/5d25d52e473f0280a3e7a354e0e141416f771ebb/src/index.ts#L39-L43

Which is working correctly:

image