I've come across an interesting puzzle. I am creating a class that can dynamically replicate itself to different levels, similar to an expandable menu. The initial definition looks like this:
class MyClass {
name: string = '';
fields: any[] = [];
next_level: MyClass = NONE_TYPE;
}
When assigning the next level, I use the following code:
next_level = new MyClass();
However, I encountered an error stating "Type 'BuiltinType' is missing the following properties from type 'MyClass': fields, ...". This means assigning NONE_TYPE in the class initialization is not possible. If I try the following:
class MyClass {
...
next_level: MyClass = new MyClass();
}
The compilation will pass, but initializing such a class will lead to stack exhaustion due to potential unlimited nesting. What would be the correct approach to handle this situation?