I am having this issue with a type mapping that is failing when mapping to a type that is the same as the parent, I.e container has children (abstract class type Base) that can be a container too or an item.
The strange thing is I have exactly the same code in another project (using a different front end framework) and it's working fine. I am not sure if there's something about CRA project that is causing the issue but nothing I have tried has allowed me to get past this issue.
https://github.com/4imble/class-transformer-recursion-issue
Here is a minimal project that demonstrates the error:
ReferenceError: Cannot access 'Container' before initialization at ./src/domain/container.ts (container.ts:12:1)
import Base from "./base";
import Item from "./item";
import { Type } from 'class-transformer';
export default class Container extends Base
{
@Type(() => Base, {
discriminator: {
property: "type",
subTypes: [
{ value: Item, name: "item" },
{ value: Container, name: "container" }
]
}
})
children: Array<Item | Container> = []
}
Long story short, I gave up, re created my project using Vite instead of CRA and it's working now. Most likely an issue with Webpack as I suspected.