Closed vahidvdn closed 2 months ago
The input type of plainToInstance
is always a class constructor, so I'm afraid it cannot output a string array if you want to use decorators.
But why is your serialized
not of array type? If entries
is typed correctly to be an array, then serialized
should be as well since plainToInstance
is overloaded.
Hello @vahidvdn,
the provided example does not work. You call plainToInstance
, but the custom transformer has { toPlainOnly: true }
, which will not run on plainToInstance
.
Please provide an expected output to make it clear what you are trying to achieve.
I assume you want
['ASP-Einrichtung', 'Deutschland']
which is not possible as you want to use a class (dto), which is not possible in javascript.
You can achieve
{
"languages": ["Deutschland", "ASP-Einrichtung"]
}
, but I do think, that class-transformer is not the best choice this case since you do not want a DTO, you only want an array. it is really simple without any lib:
const languages = entries.map(entry => entry.languages.find(language => language.language_shortcode === 'de')?.label).filter(Boolean)
Closing as answered. If you have any questions left feel free to comment on this issue.
Thank you everyone. Yes, I decided not to proceed with class-transformer for this specific user case. I just wanted to make it as declarative as possible, but it's unsuitable for this case.
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.
I'm using class transformer to change the shape of my response in my typescript application. This is my initial structure:
I want to only return German flags, where
language_shortcode
isde
.This is what I did:
This is my dto:
I get such a thing:
I don't want to repeat useless
languages
objects. How can I achieve that?Note: I can't use map to achieve what I want, because the result is array but the returned type of class transformer is
CategoryResponseDto
.