Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

svemat01
Copy link

@svemat01 svemat01 commented Jul 7, 2025

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.

Enable specifying an error type for promise actors,
providing stronger type safety for error events
emitted by these actors.
Copy link

changeset-bot bot commented Jul 7, 2025

⚠️ No Changeset found

Latest commit: e264dac

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@svemat01
Copy link
Author

svemat01 commented Jul 7, 2025

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.

@davidkpiano davidkpiano requested a review from Andarist July 7, 2025 22:15
@@ -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
Copy link
Collaborator

@Andarist Andarist Jul 8, 2025

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.

Copy link
Author

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.

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

Successfully merging this pull request may close these issues.

2 participants