Skip to content

Commit

Permalink
Limit inference sites for TEvent (#3131)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist authored Mar 7, 2022
1 parent c66b55e commit d9a0bcf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/friendly-boxes-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate': patch
---

Fixed an issue with event type being inferred from too many places within `createMachine` call and possibly ending up as `any`/`AnyEventObject` for the entire machine.
7 changes: 6 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,12 @@ export interface MachineConfig<
TAction extends BaseActionObject = BaseActionObject,
TServiceMap extends ServiceMap = ServiceMap,
TTypesMeta = TypegenDisabled
> extends StateNodeConfig<NoInfer<TContext>, TStateSchema, TEvent, TAction> {
> extends StateNodeConfig<
NoInfer<TContext>,
TStateSchema,
NoInfer<TEvent>,
TAction
> {
/**
* The initial context (extended state)
*/
Expand Down
36 changes: 36 additions & 0 deletions packages/core/test/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,42 @@ describe('context', () => {
});
});

describe('events', () => {
it('should not use actions as possible inference sites 1', () => {
const machine = createMachine({
schema: {
events: {} as {
type: 'FOO';
}
},
entry: raise('FOO')
});

const service = interpret(machine).start();

service.send({ type: 'FOO' });
// @ts-expect-error
service.send({ type: 'UNKNOWN' });
});

it('should not use actions as possible inference sites 2', () => {
const machine = createMachine({
schema: {
events: {} as {
type: 'FOO';
}
},
entry: (_ctx, _ev: any) => {}
});

const service = interpret(machine).start();

service.send({ type: 'FOO' });
// @ts-expect-error
service.send({ type: 'UNKNOWN' });
});
});

describe('interpreter', () => {
it('should be convertable to Rx observable', () => {
const state$ = from(
Expand Down

0 comments on commit d9a0bcf

Please sign in to comment.