Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tasty-chicken-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate': patch
---

Fixed an issue with inline functions in the config object used as transition actions not having their argument types inferred.
3 changes: 1 addition & 2 deletions packages/core/src/StateNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ import {
InvokeSourceDefinition,
MachineSchema,
ActorRef,
StateMachine,
InternalMachineOptions,
ServiceMap,
StateConfig,
Expand Down Expand Up @@ -306,7 +305,7 @@ class StateNode<
private _context:
| Readonly<TContext>
| (() => Readonly<TContext>) = ('context' in config
? (config as StateMachine<TContext, any, TEvent>).context
? (config as any).context
: undefined) as any, // TODO: this is unsafe, but we're removing it in v5 anyway
_stateInfo?: {
parent: StateNode<any, any, any, any, any, any>;
Expand Down
14 changes: 6 additions & 8 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,14 +449,12 @@ export type TransitionConfigOrTarget<
>;

export type TransitionsConfigMap<TContext, TEvent extends EventObject> = {
[K in TEvent['type']]?: TransitionConfigOrTarget<
TContext,
TEvent extends { type: K } ? TEvent : never
>;
} & {
''?: TransitionConfigOrTarget<TContext, TEvent>;
} & {
'*'?: TransitionConfigOrTarget<TContext, TEvent>;
[K in TEvent['type'] | '' | '*']?: K extends '' | '*'
? TransitionConfigOrTarget<TContext, TEvent>
: TransitionConfigOrTarget<
TContext,
TEvent extends { type: K } ? TEvent : never
>;
};

type TransitionsConfigArray<TContext, TEvent extends EventObject> = Array<
Expand Down
51 changes: 51 additions & 0 deletions packages/core/test/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,57 @@ describe('events', () => {

acceptMachine(toggleMachine);
});

it('should infer inline function parameters when narrowing transition actions based on the event type', () => {
createMachine({
schema: {
context: {} as {
count: number;
},
events: {} as
| { type: 'EVENT_WITH_FLAG'; flag: boolean }
| {
type: 'EVENT_WITHOUT_FLAG';
}
},
on: {
EVENT_WITH_FLAG: {
actions: (_context, event) => {
((_accept: 'EVENT_WITH_FLAG') => {})(event.type);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is pretty clever!

((_accept: boolean) => {})(event.flag);
// @ts-expect-error
((_accept: 'is not any') => {})(event);
}
}
}
});
});

it('should infer inline function parameters when for a wildcard transition', () => {
createMachine({
schema: {
context: {} as {
count: number;
},
events: {} as
| { type: 'EVENT_WITH_FLAG'; flag: boolean }
| {
type: 'EVENT_WITHOUT_FLAG';
}
},
on: {
'*': {
actions: (_context, event) => {
((_accept: 'EVENT_WITH_FLAG' | 'EVENT_WITHOUT_FLAG') => {})(
event.type
);
// @ts-expect-error
((_accept: 'is not any') => {})(event);
}
}
}
});
});
});

describe('interpreter', () => {
Expand Down