Skip to content

feat: handle array of unkown types #169

Open
@lucaswxp

Description

@lucaswxp

Hey guys.

I need to deserialize an array which holds many objects of many different types.

The type of the object inside the array is defined by a property which I can use to find the appropriate constructor. The problem is I don't how to make class-transform do this:

interface SomeAbstract{}

class Concrete1 implements SomeAbstract {
    type = 'concrete1'
}

class Concrete2 implements SomeAbstract {
    type = 'concrete2'
}

class SerializableObject {

    @Type((opt) => {
        const myobj = <any>opt.object
        if(myobj.type == 'concrete1') return Concrete1
        if(myobj.type == 'concrete2') return Concrete1
        throw 'unkown type!'
    })
    manyTypes: SomeAbstract[]
}

The problem happens because opt.object refers to the array itself, and not each item of the array. How can I overcome this?

Regards.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: has PRIssues which has a related PR closing the issue.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