Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Allow any action if action creators are not specified in model #2632

Merged
merged 2 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions .changeset/slow-timers-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
'xstate': patch
---

A regression was fixed where actions were being typed as `never` if events were specified in `createModel(...)` but not actions:

```ts
const model = createModel(
{},
{
events: {}
}
);

model.createMachine({
// These actions will cause TS to not compile
entry: 'someAction',
exit: { type: 'someObjectAction' }
});
```
8 changes: 5 additions & 3 deletions packages/core/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ export function createModel<
TComputedEvent = 'events' extends keyof TFinalModelCreators
? UnionFromCreatorsReturnTypes<TFinalModelCreators['events']>
: never,
TComputedAction = 'actions' extends keyof TFinalModelCreators
? UnionFromCreatorsReturnTypes<TFinalModelCreators['actions']>
: never
TComputedAction = 'actions' extends keyof TModelCreators
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change has completely broken the inference of actions:

const model = createModel({ foo: 100 }, {
  events: {
    BAR: () => ({})
  }
})

model.createMachine({
  // ctx is being any
  entry: (ctx) => {},
  exit: assign({
    // ctx is being unknown
    foo: (ctx) => 42
  })
})

? 'actions' extends keyof TFinalModelCreators
? UnionFromCreatorsReturnTypes<TFinalModelCreators['actions']>
: any
: any
>(
initialContext: TContext,
creators: TModelCreators
Expand Down
27 changes: 26 additions & 1 deletion packages/core/test/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,14 @@ describe('createModel', () => {
});

it('should typecheck `createMachine` for model without creators', () => {
const toggleModel = createModel({ count: 0 });
const toggleModel = createModel(
{ count: 0 },
{
events: {
TOGGLE: () => ({})
}
}
);

toggleModel.createMachine({
id: 'machine',
Expand Down Expand Up @@ -519,6 +526,24 @@ describe('createModel', () => {
});
});

it('should allow any action if actions are not specified', () => {
const model = createModel(
{},
{
events: {}
}
);

model.createMachine({
entry: 'someAction',
exit: { type: 'someObjectAction' },
on: {
// @ts-expect-error
UNEXPECTED_EVENT: {}
}
});
});

it('should keep the context type on the state after using `state.matches`', () => {
const model = createModel<{ count: number }, { type: 'INC' }>({ count: 0 });

Expand Down