How to add DoneActorEvent
types to guard
definitions in setup
?
#5129
-
I'm having trouble with upgrading from v4 to v5. One of my problems is that "named" guard methods in the I'm not exactly sure what's considered best-practice for typing such an event in this case. Here is the full proposed structure: in TS playground Abbreviated annotation here: setup({
types: {
context: {}
events: {} as { type: "DO.RETRY"; thing: number },
input: {}
// ...
},
actors:{ someInvokedPromise: fromPromise(async ()=>{
/** Pretend this is fetching from an API that can return true or false */
return {isDone: true};
})},
guards: {
isThisDone: ({event}) => {
/**
* How do I get the `DoneActorEvent` typing in here?
* TS complains that "Property 'output' does not exist on type `{ type: "DO.RETRY"}`
*/
return event.output.isDone;
},
},
}).createMachine({
// ...
states: {
idle: {
description: "Start the machine here",
invoke: {
id: "someInvokedPromise",
src: "someInvokedPromise",
onDone: [
{
guard: { type: "isThisDone" }, // Do I pass a param? Do I have to do that for every guard and every similar invoked promise?
target:"done"
},
// ...
]
}
},
// ...
}
}); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I don't recommend trying to infer Instead, use params: guards: {
isThisDone: (_, { isDone }) => {
return isDone;
}
},
// ...
onDone: {
guard: {
type: 'isDone',
params: ({ event }) => ({
isDone: event.output.isDone
})
}
} |
Beta Was this translation helpful? Give feedback.
I don't recommend trying to infer
DoneActorEvent
here, since you must assume that, unless theguard
is inline, that theguard
can be called in any transition.Instead, use params: