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: Enqueued Actions throw type error inside setup() #4545

Closed
KrisCoulson opened this issue Dec 3, 2023 · 6 comments · Fixed by #4750
Closed

Bug: Enqueued Actions throw type error inside setup() #4545

KrisCoulson opened this issue Dec 3, 2023 · 6 comments · Fixed by #4750
Assignees

Comments

@KrisCoulson
Copy link

Description

When using the new setup api to provide actions when you use the enqueueActions action typescript throws

Expected result

This should not throw an error. It does not throw an error when it is used as an object passed to createMachine

image

Actual result

image

Reproduction

https://codesandbox.io/p/sandbox/silly-danny-vf63fc

Additional context

Also just incase demo doesn't show it when you goto the repro it was being weird when I would refresh and it would show the right type but when I commented it out it would break again  🤔

Area.mp4
@Andarist
Copy link
Member

Andarist commented Dec 3, 2023

Unfortunately, we are hitting a limitation in TypeScript right now. I'd be happy to review any PR that fixes this but I already tried extensively to fix this issue with no luck. I will be working towards fixing this within the TS compiler but that will take some time to do, get reviewed, and released.

For the time being, we need to classify this as a limitation that we are aware of and add a note about this in the docs or something. enqueueActions can't be used within setup - but it can be used within createMachine chained off setup

@Jgordon-pencilwrench
Copy link

Jgordon-pencilwrench commented Jan 16, 2024

To expand on the (temporary?) workaround, use enqueueActions as an inline action within your event's action config, like:

 CANCEL_EDITING: {
            target: "editingComplete",
            actions: [
              enqueueActions(({ context, event, enqueue, check }) => {
                if (
                  check({
                    type: "formIsEmpty",
                    params: {
                      formData: context.formData,
                      additionalInformation: context.additionalInformation,
                    },
                  })
                ) {
                  enqueue({
                    type: "sendDeleteThisComplaintEventToParent",
                    params: { machineUuid: context.machineUuid },
                  });
                } else {
                  enqueue({
                    type: "editingCompleted",
                    params: {
                      machineUuid: context.machineUuid,
                      summary: event.payload.summary,
                      selectedChoiceUuid: context.selectedChoiceUuid ?? "",
                    },
                  });
                }
              }),
            ],
          },

Another possible option is to use the raise action to with a conditional transition. Don't know how that looks, but it's a possible path until this is addressed.

@Andarist
Copy link
Member

#4750 fixes the reported issue. It's still not perfect but the OP's code works alright with this PR

@andrecrimb
Copy link

andrecrimb commented May 24, 2024

The issue still persists in v5.13.0. TS still complains when I try to declare an enqueued action within setup.actions.

@davidkpiano
Copy link
Member

The issue still persists in v5.13.0. TS still complains when I try to declare an enqueued action within setup.actions.

The original example does work with the latest versions. Can you please open a new issue with code that shows that it is not working?

@andrecrimb
Copy link

Done #4923 👍🏽

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants