Skip to content

Commit 0173513

Browse files
committed
Improve readme section about recursive types.
1 parent 7008811 commit 0173513

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,16 +296,16 @@ The `children` property should be declared in your class as
296296

297297
```TypeScript
298298
import { IObservableArray } from 'mobx';
299-
import { types, ISnapshottable, IModelType, IComplexType } from 'mobx-state-tree';
299+
import { types, isStateTreeNode, ISnapshottable, IModelType, IComplexType } from 'mobx-state-tree';
300300
import { mst, mstWithChildren, shim, action, ModelInterface } from 'classy-mst';
301301

302302
export const NodeData = T.model({ value: 42 });
303303
export class NodeCode extends shim(NodeData) {
304304

305305
@action
306-
addChild(value = 42) {
306+
addChild(child: Node | typeof Node.SnapshotType) {
307307
if(!this.children) this.children = Children.create();
308-
this.children.push(Node.create());
308+
this.children.push(isStateTreeNode(child) ? child : Node.create(child));
309309

310310
return(this);
311311
}
@@ -314,6 +314,7 @@ export class NodeCode extends shim(NodeData) {
314314
}
315315

316316
const { Model: Node, Children } = mstWithChildren(NodeCode, NodeData, 'Node');
317+
export type Node = typeof Node.Type;
317318
```
318319

319320
If you want to use some other name than `children` for the property, easiest is

0 commit comments

Comments
 (0)