xstate-tools
xstate-tools copied to clipboard
Bug: typegen causes type error when a `service` is a function returning a StateMachine which has Context
Description
When an entry in the services
part of the machine definition is a function returning a state machine which has context, I get a type error.
Consider the following machine defintions:
import { createMachine, assign, interpret } from "xstate";
export const childMachineWithContext = createMachine(
{
tsTypes: {} as import("./index.typegen").Typegen0,
id: "Child Machine",
initial: "initial",
context: {count: 0},
states: {
initial: {
entry: (context, event) => {console.log("in child machine", context.count)}
},
},
schema: {context: {} as {count: number}},
predictableActionArguments: true,
preserveActionOrder: true,
}
);
export const childMachineWithoutContext = createMachine(
{
tsTypes: {} as import("./index.typegen").Typegen1,
id: "Child Machine",
initial: "initial",
states: {
initial: {
entry: (context, event) => {console.log("in child machine")}
},
},
predictableActionArguments: true,
preserveActionOrder: true,
}
);
export const machine = createMachine(
{
tsTypes: {} as import("./index.typegen").Typegen2 ,
id: "Parent Machine",
initial: "initial",
states: {
initial: {
invoke: {src: 'invoke child machine'},
},
},
predictableActionArguments: true,
preserveActionOrder: true,
},
{
services: {
'invoke child machine': (_context, _event) => childMachineWithContext
},
guards: {},
delays: {},
},
);
// start machine
const service = interpret(machine).onTransition((state) => {
console.log("interpret", state.value);
});
service.start();
Expected result
There should be no error on the "invoke child machine" service binding line.
Actual result
There is an error:
Reproduction
Unfortunately, I can't do that because typegen doesn't run in CodeSandbox.
Additional context
NOTE: The error only happens if the child state machine has context and is instantiated via a function.
This works:
'invoke child machine': childMachineWithContext
and this works:
'invoke child machine': (_context, _event) => childMachineWithoutContext
Tested with XState version 4.38.2