Open taemini opened 7 years ago
Yeah, this would be quite nice. But I don't even see where you could put a decorator to make it work. I'm pretty new to TS though.
@pplink the dictionaryType
params seems not working. Is there any settings?
Hi guys!
I have removed the +1, as they don't add to the conversation, please add a thumb up emoji to the original question if you want to show your support. Thanks!
@pplink-taemin I know what you mean, but after transpile, the interface is empty, so how do you wanna tell which property is the foreign key?
I know what you mean, but after transpile, the interface is empty, so how do you wanna tell which property is the foreign key?
The received object will have it like this:
{
'photo': Photo,
'car': Car,
}
So it's doable propably, however I dont understand the clase use-case for this, as you dont want to specifiy the type for every id you recieve.
~@taemini doesn't the polymorphism covers your use case? You can take a look here: #125~ Update: after re-reading this it does not solves this use-case.
For those who would need a custom Transform to handle that dictionary case:
// Custom decorator
function DictionaryTransform<T = any>(classType: new () => T): PropertyDecorator {
// @see https://github.com/typestack/class-transformer#advanced-usage
return Transform(({ obj, key }) => {
const entities = {};
const dictionary = obj[key];
if (!dictionary) {
return;
}
Object.keys(dictionary).forEach((objKey) => {
entities[objKey] = plainToClass(classType, dictionary[objKey], {
excludeExtraneousValues: true,
enableImplicitConversion: true, // optional - depends on your needs
});
});
return entities;
});
}
Usage
@Exclude()
class Feature {
@Expose()
name: string;
}
@Exclude()
export class User {
@DictionaryTransform(Feature)
features: Dictionary<Feature>;
}
Transforming Class which have a nested Class or Class[] type properties is working well. Great Job!
But could I also transform Class with dictionary type properties?