class-transformer icon indicating copy to clipboard operation
class-transformer copied to clipboard

feature: provide constructor parameters

Open Kapcash opened this issue 4 years ago • 5 comments

Description

Let's say I have a User class with a constructor that requires a parameter.

The example is completely dummy.

class User {
   name: string;
   isAdmin: boolean;

   constructor (whiteLabelClient: Client) {
      this.isAdmin = whiteLabelClient.priority > 5
   }
}

From what I understand in the code base, it is not possible to pass any argument to the constructor when we use plainToClass.

A current workaround is to update the plain object like we want and only after deserialise it.

Proposed solution

It would be really helpful for me to be able to pass this constructor arguments from the plainToClass method like so:

plainToClass(User, userObj, {}, whiteLabelClient)

So the signature would be something like this

function plainToClass<T, V>(cls: ClassConstructor<T>, plain: V, options?: ClassTransformOptions, ...constructorArguments: any[]): T;

Kapcash avatar Apr 14 '21 13:04 Kapcash

Yeah! this is great idea

sasha172017 avatar Apr 23 '21 14:04 sasha172017

This sounds good!

mboehmhh avatar Mar 02 '22 16:03 mboehmhh

Nothing ?

lucascardial avatar Mar 05 '22 01:03 lucascardial

In dire need of this too !

eldoccc avatar Feb 08 '24 12:02 eldoccc