class-transformer icon indicating copy to clipboard operation
class-transformer copied to clipboard

fix: @Expose() class decorator not working

Open alexvallecit opened this issue 2 years ago • 3 comments

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" }

alexvallecit avatar Apr 20 '22 22:04 alexvallecit

This is because @Expose is a property decorator, not a class decorator. @alexvallecit

ChoGathK avatar Apr 24 '22 09:04 ChoGathK

If this is true, the comment here is deceptive: https://github.com/typestack/class-transformer/blob/develop/src/decorators/expose.decorator.ts#L5

spiv avatar Dec 15 '22 13:12 spiv

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

Branchverse avatar Jul 29 '23 10:07 Branchverse