getSnapshot() type is not a union - bug or feature? #4565
Unanswered
dearlordylord
asked this question in
General
Replies: 1 comment 6 replies
-
I was debating with myself how this should work when implementing this. My intuition was that it kinda should be a union but at the same time, it wouldn't be a discriminated union. Given that making it a union would be slightly more convoluted I decided to go with the easier option. This is something that we could revisit! It could impact some code out there (remember that you can't access a key that is not present in all union members) but we still might want to consider it.
Could you show me how you have wanted to match against this? |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, for
getSnapshot()
xstate returns the values with type having optional keys, i.e.{a?: ..., b?: ...}
It's described in tests here:
xstate/packages/core/test/setup.types.test.ts
Line 1224 in 3fb895f
Pasted test is:
My question is shouldn't ExpectedType instead be a union such as
{a: 'a1' | 'a2'} | {b: 'b1' | 'b2'}
(or| {}
if we assume no state is allowed)?My source of confusion is that the model
{ a?: 'a1' | 'a2'; b?: 'b1' | 'b2'; }
allows both 'a' and 'b' present, so machine (as described in types) can have both types, despite them not being declared asparallel
.I stumbled upon this behaviour trying to exhaustively match the states with
switch
.Beta Was this translation helpful? Give feedback.
All reactions