Skip to content

Commit

Permalink
Add changeset
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkpiano committed Jan 15, 2021
1 parent 3dd4050 commit d2e328f
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions .changeset/rich-terms-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
'xstate': minor
---

An opt-in `createModel()` helper has been introduced to make it easier to work with typed `context` and events.

- `createModel(initialContext)` creates a `model` object
- `model.initialContext` returns the `initialContext`
- `model.assign(assigner, event?)` creates an `assign` action that is properly scoped to the `event` in TypeScript

See https://github.com/davidkpiano/xstate/pull/1439 for more details.

```js
import { createMachine } from 'xstate';
import { createModel } from 'xstate/lib/model'; // opt-in, not part of main build

interface UserContext {
name: string;
age: number;
}

type UserEvents =
| { type: 'updateName'; value: string }
| { type: 'updateAge'; value: number }

const userModel = createModel<UserContext, UserEvents>({
name: 'David',
age: 30
});

const assignName = userModel.assign({
name: (_, e) => e.value // correctly typed to `string`
}, 'updateName'); // restrict to 'updateName' event

const machine = createMachine<UserContext, UserEvents>({
context: userModel.context,
initial: 'active',
states: {
active: {
on: {
updateName: {
actions: assignName
}
}
}
}
});
```

0 comments on commit d2e328f

Please sign in to comment.