-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add error type to Promise actors #5324
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
base: main
Are you sure you want to change the base?
Conversation
Enable specifying an error type for promise actors, providing stronger type safety for error events emitted by these actors.
|
I'm not entirely sure if this qualifies as a minor or patch semver change, so feel free to respond with the best option or make the update yourself, whichever works best. |
@@ -120,7 +124,8 @@ const controllerMap = new WeakMap<AnyActorRef, AbortController>(); | |||
export function fromPromise< | |||
TOutput, | |||
TInput = NonReducibleUnknown, | |||
TEmitted extends EventObject = EventObject | |||
TEmitted extends EventObject = EventObject, | |||
TError = unknown |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As much as I'd like to have typed errors - I don't think this is good. We absolutely can't guarantee the correctness of this type parameter. This is as good as a type cast - but it's way more dangerous because the user won't immediately realize this. In other words, it can give somebody a false sense of safety.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn’t something I expect most people to use, but as far as I know, there’s no other way to implement something like the neverthrow wrapper I created without being able to pass the error type. Ideally, this functionality would be hidden from most users while still being accessible for those who truly need it and are comfortable type casting the error type in cases where it’s known (e.g., neverthrow, effect-ts, etc.)
If you have any suggestions for alternative solutions, I'd love to hear them.
An alternative idea would be to omit the error type additions in the fromPromise function while keeping the updates in the types file. This would allow custom variants of PromiseActorLogic
and PromiseSnapshot
with the error type to remain compatible with the rest of xstate. However, this approach would still require adding the ErrorFrom
type.
Adds the ability to define an error type for Promise actors, offering more precise type details for error states.
A practical use case for this is my neverthrow actor wrapper, which enables creating a promise actor from a ResultAsync function with a known error type. This allows proper handling of results with onDone and onError, avoiding the need to match against the result in onDone, which works but is more cumbersome and less clear in the inspector.