typestack / class-transformer

Decorator-based transformation, serialization, and deserialization between objects and classes.
MIT License
6.81k stars 500 forks source link

feature: Add type checking for data passed to #1719

Closed scotch83 closed 2 months ago

scotch83 commented 3 months ago

Description

add type checking to the method signature using Partial<T> for the plain object to be transformed.

The plainToClass method signature can be improved using the Partial utility type in Typescript to give autocomplete feature when passing objects to transform as the second parameter.

Thanks for reconsidering this feature request, if you need help I can work on it and propose a PR.

Proposed solution

function plainToClass<T>(
  ctor: new (...args: any[]) => T,
  obj: Partial<T>
)
diffy0712 commented 2 months ago

Hello @scotch83,

this signature would assume you do not use renaming in @Expose, which would not be in the Partial<T> type. Also using custom transformers you could have T and plain completely different types.

Example:

class User {
   @Expose({name: 'fullName'})
   name: string;

   @Transform(({ value }) => moment(value), { toClassOnly: true })
   dateOfBirth: Moment;
}

const plain = {
  fullName: 'username',
  dateOfBirth: '2000-01-01'
}

The type checking you suggest would not work in most of the use cases of this library.

If you are in a codebase that uses the library the way that the class type always matches the plain, you can override the plainToClass function and add this type signature for you project.

Note: use plainToInstance, as plainToClass is deprecated!

I close this as invalid. If you have any questions left feel free to comment on this issue.

scotch83 commented 2 months ago

Ok, fair enough. Thanks for replying 🙏🏻

On Sun, Jul 7, 2024, 11:42 AM David Ferencz @.***> wrote:

Hello @scotch83 https://github.com/scotch83,

this signature would assume you do not use renaming in @Expose, which would not be in the Partial type. Also using custom transformers https://github.com/typestack/class-transformer?tab=readme-ov-file#additional-data-transformation you could have T and plain completely different types.

Example:

class User { @Expose({name: 'fullName'}) name: string;

@Transform(({ value }) => moment(value), { toClassOnly: true }) dateOfBirth: Moment; }

const plain = { fullName: 'username', dateOfBirth: '2000-01-01' }

The type checking you suggest would not work in most of the use cases of this library.

If you are in a codebase that uses the library the way that the class type always matches the plain, you can override the plainToClass function and add this type signature for you project.

Note: use plainToInstance, as plainToClass is deprecated!

I close this as invalid. If you have any questions left feel free to comment on this issue.

— Reply to this email directly, view it on GitHub https://github.com/typestack/class-transformer/issues/1719#issuecomment-2212388861, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACY3U6QKNQHEFRYTQZ3DLSTZLEEQXAVCNFSM6AAAAABJR3INVGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMJSGM4DQOBWGE . You are receiving this because you were mentioned.Message ID: @.***>

github-actions[bot] commented 1 month ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.