statelyai / xstate-tools

Public monorepo for XState tooling

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bug: TS and VScode extension issues with named actions invoked in an object form

ivancuric opened this issue · comments

Description

The following machine exhibits multiple issue when using the VScode extension:

import { createMachine } from "xstate";

createMachine(
  {
    schema: {
      events: {} as { type: "goToTwo" },
    },
    states: {
      one: {
        on: {
          goToTwo: {
            target: "two",
            actions: [
              {
                type: "sayHello", // no autocomplete, go to definition not working
              },
              // "sayHello", // working
            ],
          },
        },
      },
      two: {
        entry: [
          {
            type: "sayHello", // no autocomplete, go to definition not working
          },
          // "sayHello", // working
        ],
      },
    },

    initial: "one",
  },
  {
    actions: {
      // error: sayHello is never used in the machine definition
      sayHello: (ctx, e) => {  // `ctx` is `unknown`
        console.log(ctx, e);
      },
    },
  }
);

Expected result

The syntax using the BaseActionObject shape for dispatching actions should provide the same TS and editor capabilities as the string format.

Actual result

When using the BaseActionObject shape for dispatching actions, that is: { type: "actionName" } vs "actionName", multiple things fail:

  1. autocomplete in the type field
  2. go to definition
  3. action parameters are not inferred correctly (ctx, event, meta)

Interestingly, TS is happy with providing the object form, even providing the autocomplete for the type property, however this way of dispatching actions is not documented anywhere in the old or new docs as far as I've been able to see.

Reproduction

https://codesandbox.io/s/cocky-clarke-8khzl4?file=/src/index.ts:0-651

Additional context

statelyai.stately-vscode 1.14.3
"xstate": "^4.37.2"

The "parametrized actions" are not handled at all by the codegen/extension IIRC. This is certainly an issue and we plan to fix this.