Skip to content

Serializing MST Document in Creative Tool #1925

Answered by EmilTholin
seflless asked this question in Q&A
Discussion options

You must be logged in to vote

Great, thank you for the extensive example @seflless .

The Utility types documentation states in passing that:

If the correct type cannot be inferred unambiguously from a snapshot, provide a dispatcher function to determine the type.

You could e.g. check if the snapshot has a radius and instantiate it as a Circle, otherwise a Rectangle.

const Shape = types.union(
  { dispatcher: (snapshot) => 'radius' in snapshot ? Circle : Rectangle },
  Rectangle,
  Circle
);

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by EmilTholin
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1924 on June 26, 2022 17:36.