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

Bug: typegen causes type error when a service is a function returning a StateMachine which has Context #379

Open
jluxenberg opened this issue Oct 6, 2023 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@jluxenberg
Copy link

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:

CleanShot 2023-10-05 at 17 29 15

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

@jluxenberg jluxenberg added the bug Something isn't working label Oct 6, 2023
@davidkpiano davidkpiano transferred this issue from statelyai/xstate Oct 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants