Open
Description
Description
The plainToClass
transformation should be able to recognize class types without the need of using the @Type
decorator.
@Expose()
class Data {
@Expose()
public data!: string;
}
class Wrapper {
@Type(() => Data)
public elements!: Data[];
// emitted type for this is array, so to know what type to put in it we need the @Type decorator
@Expose()
public elementDecorator!: Data;
// this will have correct type, because of the decorator
public elementNoDecorator!: Data;
// wont have correct type because no metadata is emitted for it
}
console.log(
plainToClass(Wrapper, {
elements: [{ data: 'data-one' }, { data: 'data-two' }],
elementDecorator: { data: 'property-with-decorator-data' },
elementNoDecorator: { data: 'property-without-decorator-data' },
}, { enableImplicitConversion: false })
);
The above currently returns
Wrapper {
elements: [ Data { data: 'data-one' }, Data { data: 'data-two' } ],
elementDecorator: { data: 'property-with-decorator-data' },
elementNoDecorator: { data: 'property-without-decorator-data' }
}
but it should return
Wrapper {
elements: [ Data { data: 'data-one' }, Data { data: 'data-two' } ],
elementDecorator: Data { data: 'property-with-decorator-data' },
elementNoDecorator: { data: 'property-without-decorator-data' }
}
Proposed solution
There should be multiple stages of functionality:
enableKnownClassTypeConversion
should auto-convert every type which has a registered decorator (aka the developer decorated it with a class-transformer decorator)enableUnknownClassTypeConversion
should attempt to create an instance for every class type its find even if it has no decorators applied to it
We need to keep in mind that we have enableImplicitConversion
what converts everything to the target type (both primitive and class values).
This feature needs some more thinking to come up with something useful.