mobx-state-tree
mobx-state-tree copied to clipboard
Additional `undefined` case inside `union` type
Question
- [x] I've checked documentation and searched for existing issues
- [x] I tried GitHub Discussions
Why is the instance type for types.union(types.string, types.array(types.number)) inferred as string | (number[] & ...) | undefined? I thought that it should be just string | (number[] & ...) without undefined case. Is this behaviour intended or it is a mistake in typings?
Hey @iy-work - sorry it took so long to get back to you!
From my interpretation of the union docs, I think this typing is probably for the case mentioned:
If the correct type cannot be inferred unambiguously from a snapshot, provide a dispatcher function to determine the type
It seems possible that the TypeScript typings for types.union have to account for scenarios where a type cannot be inferred, and a dispatcher function isn't provided (or the function returns undefined).
Another thing that leads me to believe it is that the determineType method declares a return type of possible undefined.
Is that a satisfactory answer for you? Happy to dig in a lil more! If I don't hear back in a week or two, I will probably close out this issue.