Skip to content

feature: provide constructor parameters #695

Open
@Kapcash

Description

@Kapcash

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;

Metadata

Metadata

Assignees

No one assigned

    Labels

    flag: needs discussionIssues which needs discussion before implementation.type: featureIssues related to new features.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions