class-transformer
class-transformer copied to clipboard
fix: @Expose() class decorator not working
Description
When I apply the @Expose()
decorator to each property of my class and do the plainToInstance
it works correctly, it transforms the properties of the class as I expect it to.
But if I apply the @Expose()
decorator to the class, it does not work as I expect, it does not transform the properties of the class.
Minimal code-snippet showcasing the problem
@Expose()
class Data {
foo: string;
bar: number;
}
console.log('data: ', plainToInstance(Data, { foo: 'foo', bar: '1' }))
Expected behavior
data: { "foo": "foo", "bar": 1 }
Actual behavior
data: { "foo": "foo", "bar": "1" }
This is because @Expose
is a property decorator, not a class decorator.
@alexvallecit
If this is true, the comment here is deceptive: https://github.com/typestack/class-transformer/blob/develop/src/decorators/expose.decorator.ts#L5
Actually I managed to make it work as intended, but it needs somehting that hasn't been said before:
The decorator@Type(() => ClassType)
seem to be the key here.
Once I specified the Type of my subclasses they got entirely exposed with the @Expose
above the entire class