asserting an error event in xstate5? #4735
Unanswered
emarksamica
asked this question in
General
Replies: 2 comments
-
@emarksamica did you find a solution here? |
Beta Was this translation helpful? Give feedback.
0 replies
-
If I understand the issue correct, this might be a solution. You can define a type for actor invocation errors with a generic for the error type like so: type ActorOnErrorEvent = <
TType extends `xstate.error.actor.${string}`,
TErrorData,
> = {
type: TType;
error: TErrorData;
}; use it type Events = ActorOnErrorEvent<"xstate.error.actor.idOfActor", { foo: string; }> | //...; and then assert the event like so onDone: [
{
type: Actions.ASSIGN_ERROR_TO_CONTEXT,
params: ({ event }) => {
assertEvent(
event,
"xstate.error.actor.idOfActor",
);
return {
APIError: event.error, // Error is now `{ foo: string; }`
};
},
},
], |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I want to define a reusable action that assigns the error response from a failed invoke to context:
TypeScript balks at this though because event is typed as being of the events I've declared in setup({types: events}). Is there a means to assertEvent so TS infers the type of the event as an invocation onError? At the moment I've only found @ts-ignore as a work around.
Beta Was this translation helpful? Give feedback.
All reactions