Open
Description
I was trying to make a class that holds an array of base classes which may be of different inherited types.
I tried to use @type decorator and assumed there is a way to get an internal 'current' element of the array to be parsed but instead (and it's logical) I get a parent object where property which I put @type decorator is defined
Please check a code snipped below: aggregate object which stores an array of actions to do and these actions may be of 2 types
export enum Action {
Add = 'ADD',
Delete = 'DELETE'
}
export class ManageChildEntitiesDto {
@IsString()
public parentEntityId: string;
@IsArray()
@ValidateNested()
@Type((tho) => {
if (!tho) {
return ChildActionDto;
}
switch ((tho.object as ChildActionDto).action) { //This type cast is not correct due to tho.object is acually of ManageChildEntitiesDto type. So the following code will not fall into any of case blocks
case Action.Add:
return AddActionDto;
case Action.Delete:
return DeleteActionDto;
}
})
actions: (AddActionDto | DeleteActionDto)[];
}
export class ChildActionDto {
@IsEnum(Action)
action: Action;
}
export class AddActionDto extends ChildActionDto {
override action: Action.Add;
newEntity: UpsertFeatureVariantDto;
}
export class DeleteActionDto extends ChildActionDto {
override action: Action.Delete;
id: string;
}
So during debugging it's clearly seen that the tho.object is a parent object rather than a particular array item
Please, help me to figure out how to use this @type decorator so that I can tell class transformer what nested type to use for each array item.