-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] Strongly-typed state value (#5006)
* Refactor * Add tests * Fix tests * Changeset * Add exhaustiveness text example * WIP: stateValueMatches * Remove stateValueMatches * Un-skip test
- Loading branch information
1 parent
fa615c2
commit 1ab9745
Showing
5 changed files
with
282 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- | ||
'xstate': minor | ||
--- | ||
|
||
The state value typings for setup state machine actors (`setup({}).createMachine({ ... })`) have been improved to represent the actual expected state values. | ||
|
||
```ts | ||
const machine = setup({}).createMachine({ | ||
initial: 'green', | ||
states: { | ||
green: {}, | ||
yellow: {}, | ||
red: { | ||
initial: 'walk', | ||
states: { | ||
walk: {}, | ||
wait: {}, | ||
stop: {} | ||
} | ||
}, | ||
emergency: { | ||
type: 'parallel', | ||
states: { | ||
main: { | ||
initial: 'blinking', | ||
states: { | ||
blinking: {} | ||
} | ||
}, | ||
cross: { | ||
initial: 'blinking', | ||
states: { | ||
blinking: {} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
|
||
const actor = createActor(machine).start(); | ||
|
||
const stateValue = actor.getSnapshot().value; | ||
|
||
if (stateValue === 'green') { | ||
// ... | ||
} else if (stateValue === 'yellow') { | ||
// ... | ||
} else if ('red' in stateValue) { | ||
stateValue; | ||
// { | ||
// red: "walk" | "wait" | "stop"; | ||
// } | ||
} else { | ||
stateValue; | ||
// { | ||
// emergency: { | ||
// main: "blinking"; | ||
// cross: "blinking"; | ||
// }; | ||
// } | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.