diff --git a/packages/core/src/actors/promise.ts b/packages/core/src/actors/promise.ts index ccc920bc1d..841918d8e0 100644 --- a/packages/core/src/actors/promise.ts +++ b/packages/core/src/actors/promise.ts @@ -9,7 +9,10 @@ import { Snapshot } from '../types.ts'; -export type PromiseSnapshot = Snapshot & { +export type PromiseSnapshot = Snapshot< + TOutput, + TError +> & { input: TInput | undefined; }; @@ -19,9 +22,10 @@ const XSTATE_PROMISE_REJECT = 'xstate.promise.reject'; export type PromiseActorLogic< TOutput, TInput = unknown, - TEmitted extends EventObject = EventObject + TEmitted extends EventObject = EventObject, + TError = unknown > = ActorLogic< - PromiseSnapshot, + PromiseSnapshot, { type: string; [k: string]: unknown }, TInput, // input AnyActorSystem, @@ -61,8 +65,8 @@ export type PromiseActorLogic< * * @see {@link fromPromise} */ -export type PromiseActorRef = ActorRefFromLogic< - PromiseActorLogic +export type PromiseActorRef = ActorRefFromLogic< + PromiseActorLogic >; const controllerMap = new WeakMap(); @@ -120,7 +124,8 @@ const controllerMap = new WeakMap(); export function fromPromise< TOutput, TInput = NonReducibleUnknown, - TEmitted extends EventObject = EventObject + TEmitted extends EventObject = EventObject, + TError = unknown >( promiseCreator: ({ input, @@ -138,8 +143,8 @@ export function fromPromise< signal: AbortSignal; emit: (emitted: TEmitted) => void; }) => PromiseLike -): PromiseActorLogic { - const logic: PromiseActorLogic = { +): PromiseActorLogic { + const logic: PromiseActorLogic = { config: promiseCreator, transition: (state, event, scope) => { if (state.status !== 'active') { diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 733f92574e..30d239ba67 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -176,6 +176,19 @@ export type OutputFrom = ? (TSnapshot & { status: 'done' })['output'] : never; +export type ErrorFrom = + T extends ActorLogic< + infer TSnapshot, + infer _TEvent, + infer _TInput, + infer _TSystem, + infer _TEmitted + > + ? (TSnapshot & { status: 'error' })['error'] + : T extends ActorRef + ? (TSnapshot & { status: 'error' })['error'] + : never; + export type ActionFunction< TContext extends MachineContext, TExpressionEvent extends EventObject, @@ -420,7 +433,7 @@ export interface InvokeDefinition< | SingleOrArray< TransitionConfig< TContext, - ErrorActorEvent, + ErrorActorEvent, TEvent, TActor, TAction, @@ -670,7 +683,7 @@ type DistributeActors< | SingleOrArray< TransitionConfigOrTarget< TContext, - ErrorActorEvent, + ErrorActorEvent>, TEvent, TActor, TAction, @@ -725,7 +738,7 @@ type DistributeActors< | SingleOrArray< TransitionConfigOrTarget< TContext, - ErrorActorEvent, + ErrorActorEvent, TEvent, TActor, TAction, @@ -818,7 +831,7 @@ export type InvokeConfig< | SingleOrArray< TransitionConfigOrTarget< TContext, - ErrorActorEvent, + ErrorActorEvent, TEvent, TActor, TAction, @@ -2182,7 +2195,7 @@ export type AnyActorScope = ActorScope< export type SnapshotStatus = 'active' | 'done' | 'error' | 'stopped'; -export type Snapshot = +export type Snapshot = | { status: 'active'; output: undefined; @@ -2196,7 +2209,7 @@ export type Snapshot = | { status: 'error'; output: undefined; - error: unknown; + error: TError; } | { status: 'stopped';