Skip to content

Commit

Permalink
Fixed an issue with creatorless models not being correctly matched by…
Browse files Browse the repository at this point in the history
… createMachine's overload (#2041)
  • Loading branch information
Andarist committed Mar 27, 2021
1 parent b18dc3f commit 3330281
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-kiwis-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate': patch
---

Fixed an issue with creatorless models not being correctly matched by `createMachine`'s overload responsible for using model-induced types.
4 changes: 2 additions & 2 deletions packages/core/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Prop<T, K> = K extends keyof T ? T[K] : never;
export interface Model<
TContext,
TEvent extends EventObject,
TModelCreators = never
TModelCreators = void
> {
initialContext: TContext;
assign: <TEventType extends TEvent['type'] = TEvent['type']>(
Expand Down Expand Up @@ -79,7 +79,7 @@ type EventFromEventCreators<EventCreators> = {

export function createModel<TContext, TEvent extends EventObject>(
initialContext: TContext
): Model<TContext, TEvent, never>;
): Model<TContext, TEvent, void>;
export function createModel<
TContext,
TModelCreators extends ModelCreators<TModelCreators>,
Expand Down
21 changes: 21 additions & 0 deletions packages/core/test/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,25 @@ describe('createModel', () => {

expect(updatedState.context.age).toEqual(42);
});

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

const machine = createMachine<typeof toggleModel>({
id: 'machine',
initial: 'inactive',
// using context here is crucial to validate that it can be assigned to the inferred TContext
context: toggleModel.initialContext,
states: {
inactive: {
on: { TOGGLE: 'active' }
},
active: {
on: { TOGGLE: 'inactive' }
}
}
});

expect(machine.initialState.context.count).toBe(0);
});
});

0 comments on commit 3330281

Please sign in to comment.