xstate: Bug: Unable to type `sendBack` in `fromCallback`
XState version
XState version 5
Description
Currently, the fromCallback function takes two generics, the event type for receive and the input type but the sendBack generic type isn’t exposed.
Currently, the only way to get around this is to do the following:
import type { InvokeCallback } from 'xstate/dist/declarations/src/actors/callback';
fromCallback((({ receive, sendBack, input }) => {
// ...
}) as InvokeCallback<ReceiveEvent, SendBackEvent, InputEvent>);
Expected result
fromCallback should have a 3rd generic parameter for the sendBack event type.
Actual result
fromCallback does not have a 3rd generic parameter for the sendBack event type.
Reproduction
N/A
Additional context
No response
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 2
- Comments: 15 (1 by maintainers)
Should be released soon 🚢
I am using XState 5.18 and this works:
Thanks for your solution @phcoliveira 🥇. @Andarist is your team planing to cover this issue anytime soon?
Ah interesting! Apparently it’s not released yet. 5.17.4 is the latest on npm, and there’s still
InvokeCallbackbut noCallbackLogicFunctionyet. Merged 4 days ago - let’s see and wait.And Outch! 5.17.4 still has the InvokeCallback - but it’s not exported anymore 😆 I’ll roll back for now…
This makes think about how a child actor’s events are perceived by the parent actor. I believe they are not “perceived” at all. Not in the sense of knowing its defined interface.
I find strange that the events sent by a child are handled like this:
Because of this, the parent machine has to type itself the event
event.from.child, as if it should know about it to redefine it.I believe the events sent from a child could be handled just like the events
onDoneandonError, used for promises.In this way, I believe the “contract” between parent and child is clearer.
I did a different hack.
It seems to work fine.