i am trying to convert json response to class so i can use it with react hoook form.
This is my class:
export class MyClass {
...
@Transform(({ value }) => {
if (value === null) return "";
else return value;
})
myKey: string | number;
...
}
This is my vite config:
export default defineConfig({
plugins: [
react({
babel: {
plugins: [
["@babel/plugin-proposal-decorators", { version: "2023-11" }],
],
},
}),
],
});
But when i call plainToInstance(MyClass, data)
I get this error:
transform.decorator.ts:15 Uncaught TypeError: Cannot read properties of undefined (reading 'constructor')
This is the code where transform has problem (when i click on line 15):
export function Transform(
transformFn: (params: TransformFnParams) => any,
options: TransformOptions = {}
): PropertyDecorator {
return function (target: any, propertyName: string | Symbol): void {
defaultMetadataStorage.addTransformMetadata({
target: target.constructor, <---------------
propertyName: propertyName as string,
transformFn,
options,
});
};
}
What is the problem here? Trying to fix it for the last 3 hours.