From 5febfe83a7e5e866c0a4523ea4f86a966af7c50f Mon Sep 17 00:00:00 2001 From: David Khourshid Date: Tue, 16 Mar 2021 20:53:00 -0400 Subject: [PATCH] Add changeset --- .changeset/fair-lemons-cross.md | 47 +++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 .changeset/fair-lemons-cross.md diff --git a/.changeset/fair-lemons-cross.md b/.changeset/fair-lemons-cross.md new file mode 100644 index 0000000000..f303a99df0 --- /dev/null +++ b/.changeset/fair-lemons-cross.md @@ -0,0 +1,47 @@ +--- +'xstate': minor +--- + +Event creators can now be modeled inside of the 2nd argument of `createModel()`, and types for both `context` and `events` will be inferred properly in `createMachine()` when given the `typeof model` as the first generic parameter. + +```ts +import { createModel } from 'xstate/lib/model'; + +const userModel = createModel( + // initial context + { + name: 'David', + age: 30 + }, + // creators (just events for now) + { + events: { + updateName: (value: string) => ({ value }), + updateAge: (value: number) => ({ value }), + anotherEvent: () => ({}) // no payload + } + } +); + +const machine = createMachine({ + context: userModel.initialContext, + initial: 'active', + states: { + active: { + on: { + updateName: { + /* ... */ + }, + updateAge: { + /* ... */ + } + } + } + } +}); + +const nextState = machine.transition( + undefined, + userModel.events.updateName('David') +); +```