@typeonce/effect-machine 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/Machine.d.ts CHANGED
@@ -32,6 +32,7 @@ export type TypeId = "~effect/Machine";
32
32
  */
33
33
  export declare const TypeId: TypeId;
34
34
  declare const MachineOutputStatesTypeId: unique symbol;
35
+ declare const MachineTypeId: unique symbol;
35
36
  /**
36
37
  * Type identifier used for the synthetic event passed to startup lifecycle
37
38
  * actions.
@@ -78,16 +79,17 @@ type IsAny<A> = 0 extends (1 & A) ? true : false;
78
79
  *
79
80
  * **Gotchas**
80
81
  *
81
- * History states and declarative first-class guards are not part of the
82
- * current API. Conditional behavior can be expressed in typed handlers with
83
- * ordinary TypeScript control flow. Use `after` for cancellable state-scoped
84
- * delayed events.
82
+ * Declarative first-class guards are not part of the current API. Conditional
83
+ * behavior can be expressed in typed handlers with ordinary TypeScript control
84
+ * flow. Use `after` for cancellable state-scoped delayed events.
85
85
  *
86
86
  * @category models
87
87
  * @since 4.0.0
88
88
  */
89
89
  export interface Machine<States extends Machine.StateSchemas, Events extends ReadonlyArray<Machine.TaggedSchema>, Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never, Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], OutputStates extends Machine.StateIdentifier<States> = never, InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events> extends Pipeable {
90
90
  readonly [TypeId]: TypeId;
91
+ /** @internal Stable type-level carrier for machine protocol extraction. */
92
+ readonly [MachineTypeId]: Machine.TypeCarrier<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits, OutputStates, InputEvents>;
91
93
  /** @internal Prevents output implementation evidence from being widened. */
92
94
  readonly [MachineOutputStatesTypeId]: Readonly<Record<OutputStates, true>>;
93
95
  /**
@@ -313,22 +315,21 @@ export declare namespace Runtime {
313
315
  }
314
316
  type ExcludeCompatibleRuntime<Requirements, Events, Emits> = Requirements extends Runtime.Requirement<infer RequiredEvents, infer RequiredEmits> ? IsAny<Requirements> extends true ? Requirements : [RequiredEvents] extends [Events] ? [RequiredEmits] extends [Emits] ? never : Requirements : Requirements : Requirements;
315
317
  type IncompatibleRuntime<Requirements, Events, Emits> = Requirements extends Runtime.Requirement<infer RequiredEvents, infer RequiredEmits> ? IsAny<Requirements> extends true ? never : [RequiredEvents] extends [Events] ? [RequiredEmits] extends [Emits] ? never : Requirements : Requirements : never;
316
- declare const RuntimeCompatibilityErrorTypeId = "~effect/Machine/RuntimeCompatibilityError";
317
318
  declare const InvokeTypeId: unique symbol;
318
- type EnsureCompatibleRuntime<Requirements, Events, Emits> = [IncompatibleRuntime<Requirements, Events, Emits>] extends [
319
- never
320
- ] ? unknown : {
321
- readonly [RuntimeCompatibilityErrorTypeId]: IncompatibleRuntime<Requirements, Events, Emits>;
322
- };
323
319
  type StateDefinitionError<Message extends string> = {
324
320
  readonly "~effect/Machine/DefinitionError": Message;
325
321
  };
326
- type ValidateStateTree<States extends Machine.StateSchemas> = {
327
- readonly [Key in keyof States]: ValidateStateNode<States[Key]>;
322
+ type ActiveStateKey<States extends Machine.StateSchemas> = Machine.ActiveStateKey<States>;
323
+ type HistoryStateKey<States extends Machine.StateSchemas> = Machine.HistoryStateKey<States>;
324
+ type ValidateStateTree<States extends Machine.StateSchemas, AllowHistory extends boolean = false> = {
325
+ readonly [Key in keyof States]: ValidateStateNode<States[Key], AllowHistory>;
328
326
  };
329
- type ValidateStateNode<Node> = Node extends Machine.TaggedSchema ? unknown : Node extends {
327
+ type ValidateStateNode<Node, AllowHistory extends boolean> = Node extends Machine.HistoryStateNodeConfig ? AllowHistory extends true ? ValidateHistoryStateNode<Node> : StateDefinitionError<"History states must be declared below an active parent state"> : Node extends Machine.TaggedSchema ? unknown : Node extends {
330
328
  readonly schema: Machine.TaggedSchema;
331
329
  } ? ValidateStateNodeConfig<Node> : StateDefinitionError<"State nodes must be tagged schemas or state node configs">;
330
+ type ValidateHistoryStateNode<Node extends Machine.HistoryStateNodeConfig> = [
331
+ Extract<keyof Node, "schema" | "states" | "initial" | "output">
332
+ ] extends [never] ? unknown : StateDefinitionError<"History states cannot declare schemas, children, initial states, or output">;
332
333
  type ValidateStateNodeConfig<Node extends {
333
334
  readonly schema: Machine.TaggedSchema;
334
335
  }> = Node extends {
@@ -344,14 +345,14 @@ type ValidateStateNodeWithChildren<Node extends {
344
345
  } ? StateDefinitionError<"Final states cannot declare child states"> : Node extends {
345
346
  readonly type: "parallel";
346
347
  } ? "initial" extends keyof Node ? StateDefinitionError<"Parallel states cannot declare an initial child"> : {
347
- readonly states: ValidateStateTree<Children>;
348
+ readonly states: ValidateStateTree<Children, true>;
348
349
  } & ValidateOutputSchema<Node> : "output" extends keyof Node ? StateDefinitionError<"Only final and parallel states can declare output"> : ValidateCompoundStateNode<Node, Children> : StateDefinitionError<"Child states must be a state tree">;
349
350
  type ValidateCompoundStateNode<Node extends {
350
351
  readonly schema: Machine.TaggedSchema;
351
352
  }, Children extends Machine.StateSchemas> = Node extends {
352
353
  readonly initial: infer Initial;
353
- } ? Initial extends Extract<keyof Children, string> ? {
354
- readonly states: ValidateStateTree<Children>;
354
+ } ? Initial extends ActiveStateKey<Children> ? {
355
+ readonly states: ValidateStateTree<Children, true>;
355
356
  } : StateDefinitionError<"Compound initial must be one of its direct child keys"> : StateDefinitionError<"Compound states must declare an initial child">;
356
357
  type ValidateStateNodeWithoutChildren<Node extends {
357
358
  readonly schema: Machine.TaggedSchema;
@@ -361,7 +362,7 @@ type ValidateStateNodeWithoutChildren<Node extends {
361
362
  type DefineStateTreeInput<States extends Machine.StateSchemas> = {
362
363
  readonly [Key in keyof States]: DefineStateNodeInput<States[Key]>;
363
364
  };
364
- type DefineStateNodeInput<Node> = Node extends Machine.TaggedSchema ? Node : Node extends {
365
+ type DefineStateNodeInput<Node> = Node extends Machine.TaggedSchema ? Node : Node extends Machine.HistoryStateNodeConfig ? Machine.HistoryStateNodeConfig : Node extends {
365
366
  readonly type: "parallel";
366
367
  readonly states: infer Children extends Machine.StateSchemas;
367
368
  } ? Omit<Node, "states"> & {
@@ -369,12 +370,19 @@ type DefineStateNodeInput<Node> = Node extends Machine.TaggedSchema ? Node : Nod
369
370
  } : Node extends {
370
371
  readonly states: infer Children extends Machine.StateSchemas;
371
372
  } ? Omit<Node, "initial" | "states"> & {
372
- readonly initial: Extract<keyof Children, string>;
373
+ readonly initial: ActiveStateKey<Children>;
373
374
  readonly states: DefineStateTreeInput<Children>;
374
375
  } : Node;
375
376
  type ValidateDefinedStates<States extends Machine.StateSchemas> = [States] extends [
376
377
  Machine.ValidateStateSchemas<States>
377
378
  ] ? [] : [validation: Machine.ValidateStateSchemas<States>];
379
+ type InvalidDefinedStateTreeInput<States extends Machine.StateSchemas> = [States] extends [
380
+ Machine.ValidateStateSchemas<States>
381
+ ] ? never : States & Machine.ValidateStateSchemas<States>;
382
+ interface DefineStates {
383
+ <const States extends Machine.StateSchemas>(states: States & DefineStateTreeInput<NoInfer<States>>, ..._validation: ValidateDefinedStates<NoInfer<States>>): Machine.DefinedStates<States>;
384
+ <const States extends Machine.StateSchemas>(states: InvalidDefinedStateTreeInput<States>): never;
385
+ }
378
386
  type EventProtocolError<Message extends string, Tag extends PropertyKey = never> = {
379
387
  readonly "~effect/Machine/EventProtocolError": Message;
380
388
  readonly tag: Tag;
@@ -383,16 +391,39 @@ type DuplicateEventTag<Events extends ReadonlyArray<Machine.TaggedSchema>, Seen
383
391
  infer Head extends Machine.TaggedSchema,
384
392
  ...infer Tail extends ReadonlyArray<Machine.TaggedSchema>
385
393
  ] ? Machine.TagOf<Head> extends infer Tag extends PropertyKey ? Tag extends Seen ? Tag | DuplicateEventTag<Tail, Seen> : DuplicateEventTag<Tail, Seen | Tag> : never : never;
386
- type ValidateEventProtocol<InputEvents extends ReadonlyArray<Machine.TaggedSchema>, InternalEvents extends ReadonlyArray<Machine.TaggedSchema>, DuplicateInput extends PropertyKey = DuplicateEventTag<InputEvents>, DuplicateInternal extends PropertyKey = DuplicateEventTag<InternalEvents>, Overlap extends PropertyKey = Extract<Machine.TagOf<InputEvents[number]>, Machine.TagOf<InternalEvents[number]>>> = [DuplicateInput] extends [never] ? [DuplicateInternal] extends [never] ? [Overlap] extends [never] ? [] : [validation: EventProtocolError<"Public and internal event tags must be disjoint", Overlap>] : [validation: EventProtocolError<"Internal event tags must be unique", DuplicateInternal>] : [validation: EventProtocolError<"Public event tags must be unique", DuplicateInput>];
394
+ type ValidateInputEventProtocol<InputEvents extends ReadonlyArray<Machine.TaggedSchema>, DuplicateInput extends PropertyKey = DuplicateEventTag<InputEvents>> = [DuplicateInput] extends [never] ? unknown : EventProtocolError<"Public event tags must be unique", DuplicateInput>;
395
+ type ValidateInternalEventProtocol<InputEvents extends ReadonlyArray<Machine.TaggedSchema>, InternalEvents extends ReadonlyArray<Machine.TaggedSchema>, DuplicateInternal extends PropertyKey = DuplicateEventTag<InternalEvents>, Overlap extends PropertyKey = Extract<Machine.TagOf<InputEvents[number]>, Machine.TagOf<InternalEvents[number]>>> = [DuplicateInternal] extends [never] ? [Overlap] extends [never] ? unknown : EventProtocolError<"Public and internal event tags must be disjoint", Overlap> : EventProtocolError<"Internal event tags must be unique", DuplicateInternal>;
387
396
  declare const SnapshotBuilderStateTypeId: unique symbol;
388
- type SnapshotBuilderComplete<Regions> = {
397
+ declare const SnapshotBuilderConstructionTypeId: unique symbol;
398
+ type SnapshotBuilderComplete<Regions, Constructed extends boolean = false> = {
389
399
  readonly [SnapshotBuilderStateTypeId]: Regions;
400
+ readonly [SnapshotBuilderConstructionTypeId]: Constructed;
390
401
  };
402
+ type FromCallable<Arguments extends ReadonlyArray<unknown>, Result> = Arguments extends readonly [infer Input, ...infer Rest extends ReadonlyArray<unknown>] ? {} extends Input ? {
403
+ (...args: Rest): Result;
404
+ (...args: Arguments): Result;
405
+ } : (...args: Arguments) => Result : (...args: Arguments) => Result;
406
+ type FromMethod<Arguments extends ReadonlyArray<unknown>, Result> = {
407
+ /**
408
+ * Constructs the selected state from its schema make input while the
409
+ * machine plans the resulting configuration. The input may be omitted when
410
+ * the schema accepts an empty constructor object.
411
+ *
412
+ * @since 4.0.0
413
+ */
414
+ readonly from: FromCallable<Arguments, Machine.StateConstruction<Result>>;
415
+ };
416
+ type ConstructionResult<Result> = Result | Machine.StateConstruction<Result>;
417
+ type UnwrapConstruction<Result> = Result extends Machine.StateConstruction<infer Value> ? Value : Result;
418
+ type ConstructionSelectorFromCallable<Input, Builder, Result> = {} extends Input ? {
419
+ <Selected extends ConstructionResult<Result>>(state: (builder: Builder) => Selected): Machine.StateConstruction<UnwrapConstruction<Selected>>;
420
+ <Selected extends ConstructionResult<Result>>(input: Input, state: (builder: Builder) => Selected): Machine.StateConstruction<UnwrapConstruction<Selected>>;
421
+ } : <Selected extends ConstructionResult<Result>>(input: Input, state: (builder: Builder) => Selected) => Machine.StateConstruction<UnwrapConstruction<Selected>>;
391
422
  type InitialSnapshotBuilderWithPrefix<States extends Machine.StateSchemas, Prefix extends string = ""> = {
392
- readonly [Key in Extract<keyof States, string>]: InitialSnapshotMethod<States, Key, Prefix>;
423
+ readonly [Key in ActiveStateKey<States>]: InitialSnapshotMethod<States, Key, Prefix>;
393
424
  };
394
- type InitialSnapshotMethod<States extends Machine.StateSchemas, StateId extends Extract<keyof States, string>, Prefix extends string> = (...args: InitialSnapshotArguments<States, StateId, Prefix>) => InitialSnapshotResult<States, StateId, Prefix>;
395
- type InitialSnapshotArguments<States extends Machine.StateSchemas, StateId extends Extract<keyof States, string>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends infer Node ? Node extends {
425
+ type InitialSnapshotMethod<States extends Machine.StateSchemas, StateId extends ActiveStateKey<States>, Prefix extends string> = ((...args: InitialSnapshotArguments<States, StateId, Prefix>) => InitialSnapshotResult<States, StateId, Prefix>) & FromMethod<InitialSnapshotFromArguments<States, StateId, Prefix>, InitialSnapshotResult<States, StateId, Prefix>>;
426
+ type InitialSnapshotArguments<States extends Machine.StateSchemas, StateId extends ActiveStateKey<States>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends infer Node ? Node extends {
396
427
  readonly type: "parallel";
397
428
  readonly states: infer Children extends Machine.StateSchemas;
398
429
  } ? [
@@ -401,32 +432,50 @@ type InitialSnapshotArguments<States extends Machine.StateSchemas, StateId exten
401
432
  ] : Node extends {
402
433
  readonly states: infer Children extends Machine.StateSchemas;
403
434
  } ? Node extends {
404
- readonly initial: infer Initial extends Extract<keyof Children, string>;
435
+ readonly initial: infer Initial extends ActiveStateKey<Children>;
405
436
  } ? [
406
437
  value: Machine.NodeSchema<Node>["Type"],
407
438
  state: (builder: Pick<InitialSnapshotBuilderWithPrefix<Children, Path>, Initial>) => InitialSnapshotResult<Children, Initial, Path>
408
439
  ] : never : [value: Machine.NodeSchema<Node>["Type"]] : never;
409
- type InitialSnapshotResult<States extends Machine.StateSchemas, StateId extends Extract<keyof States, string>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends infer Node ? Node extends {
440
+ type InitialSnapshotFromArguments<States extends Machine.StateSchemas, StateId extends ActiveStateKey<States>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends infer Node ? Node extends {
441
+ readonly type: "parallel";
442
+ readonly states: infer Children extends Machine.StateSchemas;
443
+ } ? [
444
+ input: Machine.NodeSchema<Node>["~type.make.in"],
445
+ states: (builder: InitialParallelBuilder<Children, Path>) => SnapshotBuilderComplete<InitialSnapshotRegionsWithPrefix<Children, Path>, boolean>
446
+ ] : Node extends {
447
+ readonly states: infer Children extends Machine.StateSchemas;
448
+ } ? Node extends {
449
+ readonly initial: infer Initial extends ActiveStateKey<Children>;
450
+ } ? [
451
+ input: Machine.NodeSchema<Node>["~type.make.in"],
452
+ state: (builder: Pick<InitialSnapshotBuilderWithPrefix<Children, Path>, Initial>) => ConstructionResult<InitialSnapshotResult<Children, Initial, Path>>
453
+ ] : never : [input: Machine.NodeSchema<Node>["~type.make.in"]] : never;
454
+ type InitialSnapshotResult<States extends Machine.StateSchemas, StateId extends ActiveStateKey<States>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends infer Node ? Node extends {
410
455
  readonly type: "parallel";
411
456
  readonly states: infer Children extends Machine.StateSchemas;
412
457
  } ? Machine.ParallelSnapshot<Path, Machine.NodeSchema<Node>["Type"], InitialSnapshotRegionsWithPrefix<Children, Path>> : Node extends {
413
458
  readonly states: infer Children extends Machine.StateSchemas;
414
459
  } ? Node extends {
415
- readonly initial: infer Initial extends Extract<keyof Children, string>;
460
+ readonly initial: infer Initial extends ActiveStateKey<Children>;
416
461
  } ? Machine.CompoundSnapshot<Path, Machine.NodeSchema<Node>["Type"], InitialSnapshotResult<Children, Initial, Path>> : never : Machine.AtomicSnapshot<Path, Machine.NodeSchema<Node>["Type"]> : never;
417
462
  type InitialSnapshotRegionsWithPrefix<States extends Machine.StateSchemas, Prefix extends string> = {
418
- readonly [Key in Extract<keyof States, string>]: InitialSnapshotResult<States, Key, Prefix>;
463
+ readonly [Key in ActiveStateKey<States>]: InitialSnapshotResult<States, Key, Prefix>;
419
464
  };
420
- type InitialParallelBuilder<States extends Machine.StateSchemas, Prefix extends string, Remaining extends Extract<keyof States, string> = Extract<keyof States, string>, Regions = {}> = SnapshotBuilderComplete<Regions> & {
421
- readonly [Key in Remaining]: (...args: InitialSnapshotArguments<States, Key, Prefix>) => InitialParallelBuilder<States, Prefix, Exclude<Remaining, Key>, Regions & {
465
+ type InitialParallelBuilder<States extends Machine.StateSchemas, Prefix extends string, Remaining extends ActiveStateKey<States> = ActiveStateKey<States>, Regions = {}, Constructed extends boolean = false> = SnapshotBuilderComplete<Regions, Constructed> & {
466
+ readonly [Key in Remaining]: ((...args: InitialSnapshotArguments<States, Key, Prefix>) => InitialParallelBuilder<States, Prefix, Exclude<Remaining, Key>, Regions & {
422
467
  readonly [Region in Key]: InitialSnapshotResult<States, Key, Prefix>;
423
- }>;
468
+ }, Constructed>) & {
469
+ readonly from: FromCallable<InitialSnapshotFromArguments<States, Key, Prefix>, InitialParallelBuilder<States, Prefix, Exclude<Remaining, Key>, Regions & {
470
+ readonly [Region in Key]: InitialSnapshotResult<States, Key, Prefix>;
471
+ }, true>>;
472
+ };
424
473
  };
425
474
  type FullSnapshotBuilderWithPrefix<States extends Machine.StateSchemas, Prefix extends string = ""> = {
426
- readonly [Key in Extract<keyof States, string>]: FullSnapshotMethod<States, Key, Prefix>;
475
+ readonly [Key in ActiveStateKey<States>]: FullSnapshotMethod<States, Key, Prefix>;
427
476
  };
428
- type FullSnapshotMethod<States extends Machine.StateSchemas, StateId extends Extract<keyof States, string>, Prefix extends string> = (...args: FullSnapshotArguments<States, StateId, Prefix>) => FullSnapshotResult<States, StateId, Prefix>;
429
- type FullSnapshotArguments<States extends Machine.StateSchemas, StateId extends Extract<keyof States, string>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends infer Node ? Node extends {
477
+ type FullSnapshotMethod<States extends Machine.StateSchemas, StateId extends ActiveStateKey<States>, Prefix extends string> = ((...args: FullSnapshotArguments<States, StateId, Prefix>) => FullSnapshotResult<States, StateId, Prefix>) & FromMethod<FullSnapshotFromArguments<States, StateId, Prefix>, FullSnapshotResult<States, StateId, Prefix>>;
478
+ type FullSnapshotArguments<States extends Machine.StateSchemas, StateId extends ActiveStateKey<States>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends infer Node ? Node extends {
430
479
  readonly type: "parallel";
431
480
  readonly states: infer Children extends Machine.StateSchemas;
432
481
  } ? [
@@ -438,11 +487,27 @@ type FullSnapshotArguments<States extends Machine.StateSchemas, StateId extends
438
487
  value: Machine.NodeSchema<Node>["Type"],
439
488
  state: (builder: FullSnapshotBuilderWithPrefix<Children, Path>) => Machine.SnapshotWithPrefix<Children, Path>
440
489
  ] : [value: Machine.NodeSchema<Node>["Type"]] : never;
441
- type FullSnapshotResult<States extends Machine.StateSchemas, StateId extends Extract<keyof States, string>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = Machine.SnapshotByIdentifierWithPath<States, StateId, Path>;
442
- type FullParallelBuilder<States extends Machine.StateSchemas, Prefix extends string, Remaining extends Extract<keyof States, string> = Extract<keyof States, string>, Regions = {}> = SnapshotBuilderComplete<Regions> & {
443
- readonly [Key in Remaining]: (...args: FullSnapshotArguments<States, Key, Prefix>) => FullParallelBuilder<States, Prefix, Exclude<Remaining, Key>, Regions & {
490
+ type FullSnapshotFromArguments<States extends Machine.StateSchemas, StateId extends ActiveStateKey<States>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends infer Node ? Node extends {
491
+ readonly type: "parallel";
492
+ readonly states: infer Children extends Machine.StateSchemas;
493
+ } ? [
494
+ input: Machine.NodeSchema<Node>["~type.make.in"],
495
+ states: (builder: FullParallelBuilder<Children, Path>) => SnapshotBuilderComplete<Machine.SnapshotRegionsWithPrefix<Children, Path>, boolean>
496
+ ] : Node extends {
497
+ readonly states: infer Children extends Machine.StateSchemas;
498
+ } ? [
499
+ input: Machine.NodeSchema<Node>["~type.make.in"],
500
+ state: (builder: FullSnapshotBuilderWithPrefix<Children, Path>) => ConstructionResult<Machine.SnapshotWithPrefix<Children, Path>>
501
+ ] : [input: Machine.NodeSchema<Node>["~type.make.in"]] : never;
502
+ type FullSnapshotResult<States extends Machine.StateSchemas, StateId extends ActiveStateKey<States>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = Machine.SnapshotByIdentifierWithPath<States, StateId, Path>;
503
+ type FullParallelBuilder<States extends Machine.StateSchemas, Prefix extends string, Remaining extends ActiveStateKey<States> = ActiveStateKey<States>, Regions = {}, Constructed extends boolean = false> = SnapshotBuilderComplete<Regions, Constructed> & {
504
+ readonly [Key in Remaining]: ((...args: FullSnapshotArguments<States, Key, Prefix>) => FullParallelBuilder<States, Prefix, Exclude<Remaining, Key>, Regions & {
444
505
  readonly [Region in Key]: FullSnapshotResult<States, Key, Prefix>;
445
- }>;
506
+ }, Constructed>) & {
507
+ readonly from: FromCallable<FullSnapshotFromArguments<States, Key, Prefix>, FullParallelBuilder<States, Prefix, Exclude<Remaining, Key>, Regions & {
508
+ readonly [Region in Key]: FullSnapshotResult<States, Key, Prefix>;
509
+ }, true>>;
510
+ };
446
511
  };
447
512
  type ParentPath<Path extends string> = Path extends `${infer Parent}.${infer Child}` ? Child extends `${string}.${string}` ? `${Parent}.${ParentPath<Child>}` : Parent : never;
448
513
  type IsCompoundNode<Node> = Node extends {
@@ -455,42 +520,104 @@ type ChildrenOf<States extends Machine.StateSchemas, Path extends Machine.StateI
455
520
  readonly states: infer Children extends Machine.StateSchemas;
456
521
  } ? Children : never;
457
522
  type StateIdentifierFromPath<States extends Machine.StateSchemas, Path extends string> = Extract<Path, Machine.StateIdentifier<States>>;
458
- type LocalTargetResult<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, StateId extends Extract<keyof States, string>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends {
523
+ type LocalTargetResult<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, StateId extends ActiveStateKey<States>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends {
459
524
  readonly states: infer Children extends Machine.StateSchemas;
460
- } ? LocalTargetResultWithPrefix<AllStates, Children, Path> : Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>;
525
+ } ? States[StateId] extends {
526
+ readonly type: "parallel";
527
+ } ? Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>> : LocalTargetResultWithPrefix<AllStates, Children, Path> : Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>;
461
528
  type LocalTargetResultWithPrefix<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, Prefix extends string> = {
462
- readonly [Key in Extract<keyof States, string>]: LocalTargetResult<AllStates, States, Key, Prefix>;
463
- }[Extract<keyof States, string>];
464
- type LocalTargetBuilderWithPrefix<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, Prefix extends string> = {
465
- readonly [Key in Extract<keyof States, string>]: LocalTargetMethod<AllStates, States, Key, Prefix>;
529
+ readonly [Key in ActiveStateKey<States>]: LocalTargetResult<AllStates, States, Key, Prefix>;
530
+ }[ActiveStateKey<States>];
531
+ type LocalTargetBuilderWithPrefix<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, Prefix extends string, Source extends Machine.StateIdentifier<AllStates>> = {
532
+ readonly [Key in ActiveStateKey<States>]: LocalTargetMethod<AllStates, States, Key, Prefix, Source>;
466
533
  };
467
- type LocalTargetMethod<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, StateId extends Extract<keyof States, string>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends infer Node ? Node extends {
534
+ type LocalTargetMethod<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, StateId extends ActiveStateKey<States>, Prefix extends string, Source extends Machine.StateIdentifier<AllStates>, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends infer Node ? Node extends {
535
+ readonly type: "parallel";
468
536
  readonly states: infer Children extends Machine.StateSchemas;
469
- } ? <Result extends LocalTargetResultWithPrefix<AllStates, Children, Path>>(value: Machine.NodeSchema<Node>["Type"], state: (builder: LocalTargetBuilderWithPrefix<AllStates, Children, Path>) => Result) => Result : (value: Machine.NodeSchema<Node>["Type"]) => Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>> : never;
470
- type LocalTargetBuilderForScope<States extends Machine.StateSchemas, Scope extends Machine.StateIdentifier<States>> = ChildrenOf<States, Scope> extends infer Children extends Machine.StateSchemas ? LocalTargetBuilderWithPrefix<States, Children, Scope> & {
537
+ } ? Source extends Path | `${Path}.${string}` ? (<Result extends ConstructionResult<LocalTargetResultWithPrefix<AllStates, Children, Path>>>(value: Machine.NodeSchema<Node>["Type"], state: (builder: LocalTargetBuilderWithPrefix<AllStates, Children, Path, Source>) => Result) => Result) & {
538
+ readonly from: ConstructionSelectorFromCallable<Machine.NodeSchema<Node>["~type.make.in"], LocalTargetBuilderWithPrefix<AllStates, Children, Path, Source>, LocalTargetResultWithPrefix<AllStates, Children, Path>>;
539
+ } : ((value: Machine.NodeSchema<Node>["Type"], states: (builder: FullParallelBuilder<Children, Path>) => SnapshotBuilderComplete<Machine.SnapshotRegionsWithPrefix<Children, Path>>) => Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>) & FromMethod<[
540
+ input: Machine.NodeSchema<Node>["~type.make.in"],
541
+ states: (builder: FullParallelBuilder<Children, Path>) => SnapshotBuilderComplete<Machine.SnapshotRegionsWithPrefix<Children, Path>, boolean>
542
+ ], Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>> : Node extends {
543
+ readonly states: infer Children extends Machine.StateSchemas;
544
+ } ? (<Result extends ConstructionResult<LocalTargetResultWithPrefix<AllStates, Children, Path>>>(value: Machine.NodeSchema<Node>["Type"], state: (builder: LocalTargetBuilderWithPrefix<AllStates, Children, Path, Source>) => Result) => Result) & {
545
+ readonly from: ConstructionSelectorFromCallable<Machine.NodeSchema<Node>["~type.make.in"], LocalTargetBuilderWithPrefix<AllStates, Children, Path, Source>, LocalTargetResultWithPrefix<AllStates, Children, Path>>;
546
+ } : ((value: Machine.NodeSchema<Node>["Type"]) => Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>) & FromMethod<[
547
+ input: Machine.NodeSchema<Node>["~type.make.in"]
548
+ ], Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>> : never;
549
+ type LocalTargetBuilderForScope<States extends Machine.StateSchemas, Scope extends Machine.StateIdentifier<States>, Source extends Machine.StateIdentifier<States>> = ChildrenOf<States, Scope> extends infer Children extends Machine.StateSchemas ? LocalTargetBuilderWithPrefix<States, Children, Scope, Source> & {
471
550
  /**
472
551
  * Updates the value of the state containing the local group and moves to
473
552
  * one of the states inside it. Values in other active branches are kept.
474
553
  *
475
554
  * @since 4.0.0
476
555
  */
477
- readonly with: <Result extends LocalTargetResultWithPrefix<States, Children, Scope>>(value: Machine.StateByIdentifier<States, Scope>, state: (builder: LocalTargetBuilderWithPrefix<States, Children, Scope>) => Result) => Result;
556
+ readonly with: (<Result extends ConstructionResult<LocalTargetResultWithPrefix<States, Children, Scope>>>(value: Machine.StateByIdentifier<States, Scope>, state: (builder: LocalTargetBuilderWithPrefix<States, Children, Scope, Source>) => Result) => Result) & {
557
+ readonly from: ConstructionSelectorFromCallable<Machine.SchemaByIdentifier<States, Scope>["~type.make.in"], LocalTargetBuilderWithPrefix<States, Children, Scope, Source>, LocalTargetResultWithPrefix<States, Children, Scope>>;
558
+ };
478
559
  } : {};
479
- type BranchTargetResult<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, StateId extends Extract<keyof States, string>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends {
560
+ type BranchTargetResult<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, StateId extends ActiveStateKey<States>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends {
480
561
  readonly states: infer Children extends Machine.StateSchemas;
481
- } ? BranchTargetResultWithPrefix<AllStates, Children, Path> : Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>;
562
+ } ? States[StateId] extends {
563
+ readonly type: "parallel";
564
+ } ? Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>> : BranchTargetResultWithPrefix<AllStates, Children, Path> : Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>;
482
565
  type BranchTargetResultWithPrefix<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, Prefix extends string> = {
483
- readonly [Key in Extract<keyof States, string>]: BranchTargetResult<AllStates, States, Key, Prefix>;
484
- }[Extract<keyof States, string>];
485
- type BranchTargetBuilderWithPrefix<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, Prefix extends string> = {
486
- readonly [Key in Extract<keyof States, string>]: BranchTargetMethod<AllStates, States, Key, Prefix>;
566
+ readonly [Key in ActiveStateKey<States>]: BranchTargetResult<AllStates, States, Key, Prefix>;
567
+ }[ActiveStateKey<States>];
568
+ type BranchTargetBuilderWithPrefix<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, Prefix extends string, Source extends Machine.StateIdentifier<AllStates>> = {
569
+ readonly [Key in ActiveStateKey<States>]: BranchTargetMethod<AllStates, States, Key, Prefix, Source>;
487
570
  };
488
- type BranchTargetMethod<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, StateId extends Extract<keyof States, string>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends infer Node ? Node extends {
571
+ type BranchTargetMethod<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, StateId extends ActiveStateKey<States>, Prefix extends string, Source extends Machine.StateIdentifier<AllStates>, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends infer Node ? Node extends {
572
+ readonly type: "parallel";
489
573
  readonly states: infer Children extends Machine.StateSchemas;
490
- } ? (<Result extends BranchTargetResultWithPrefix<AllStates, Children, Path>>(value: Machine.NodeSchema<Node>["Type"], state: (builder: BranchTargetBuilderWithPrefix<AllStates, Children, Path>) => Result) => Result) & BranchTargetBuilderWithPrefix<AllStates, Children, Path> : (value: Machine.NodeSchema<Node>["Type"]) => Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>> : never;
491
- type BranchTargetBuilderForRoot<States extends Machine.StateSchemas, Root extends Extract<keyof States, string>> = {
492
- readonly [Key in Root]: BranchTargetMethod<States, States, Key, "">;
574
+ } ? Source extends Path | `${Path}.${string}` ? (<Result extends ConstructionResult<BranchTargetResultWithPrefix<AllStates, Children, Path>>>(value: Machine.NodeSchema<Node>["Type"], state: (builder: BranchTargetBuilderWithPrefix<AllStates, Children, Path, Source>) => Result) => Result) & {
575
+ readonly from: ConstructionSelectorFromCallable<Machine.NodeSchema<Node>["~type.make.in"], BranchTargetBuilderWithPrefix<AllStates, Children, Path, Source>, BranchTargetResultWithPrefix<AllStates, Children, Path>>;
576
+ } & BranchTargetBuilderWithPrefix<AllStates, Children, Path, Source> : ((value: Machine.NodeSchema<Node>["Type"], states: (builder: FullParallelBuilder<Children, Path>) => SnapshotBuilderComplete<Machine.SnapshotRegionsWithPrefix<Children, Path>>) => Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>) & FromMethod<[
577
+ input: Machine.NodeSchema<Node>["~type.make.in"],
578
+ states: (builder: FullParallelBuilder<Children, Path>) => SnapshotBuilderComplete<Machine.SnapshotRegionsWithPrefix<Children, Path>, boolean>
579
+ ], Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>> : Node extends {
580
+ readonly states: infer Children extends Machine.StateSchemas;
581
+ } ? (<Result extends ConstructionResult<BranchTargetResultWithPrefix<AllStates, Children, Path>>>(value: Machine.NodeSchema<Node>["Type"], state: (builder: BranchTargetBuilderWithPrefix<AllStates, Children, Path, Source>) => Result) => Result) & {
582
+ readonly from: ConstructionSelectorFromCallable<Machine.NodeSchema<Node>["~type.make.in"], BranchTargetBuilderWithPrefix<AllStates, Children, Path, Source>, BranchTargetResultWithPrefix<AllStates, Children, Path>>;
583
+ } & BranchTargetBuilderWithPrefix<AllStates, Children, Path, Source> : ((value: Machine.NodeSchema<Node>["Type"]) => Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>) & FromMethod<[
584
+ input: Machine.NodeSchema<Node>["~type.make.in"]
585
+ ], Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>> : never;
586
+ type BranchTargetBuilderForRoot<States extends Machine.StateSchemas, Root extends ActiveStateKey<States>, Source extends Machine.StateIdentifier<States>> = {
587
+ readonly [Key in Root]: BranchTargetMethod<States, States, Key, "", Source>;
588
+ };
589
+ type HistoryContainingKey<States extends Machine.StateSchemas> = {
590
+ readonly [Key in Extract<keyof States, string>]: States[Key] extends Machine.HistoryStateNodeConfig ? Key : States[Key] extends {
591
+ readonly states: infer Children extends Machine.StateSchemas;
592
+ } ? [
593
+ Machine.HistoryIdentifier<Children>
594
+ ] extends [never] ? never : Key : never;
595
+ }[Extract<keyof States, string>];
596
+ type HistoryTargetBuilderWithPrefix<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, Prefix extends string> = {
597
+ readonly [Key in HistoryContainingKey<States>]: States[Key] extends Machine.HistoryStateNodeConfig ? () => Machine.HistoryTarget<AllStates, Extract<Machine.JoinPath<Prefix, Key>, Machine.HistoryIdentifier<AllStates>>> : States[Key] extends {
598
+ readonly states: infer Children extends Machine.StateSchemas;
599
+ } ? HistoryTargetBuilderWithPrefix<AllStates, Children, Machine.JoinPath<Prefix, Key>> : never;
493
600
  };
601
+ type HasDirectShallowHistory<States extends Machine.StateSchemas> = {
602
+ readonly [Key in HistoryStateKey<States>]: States[Key] extends {
603
+ readonly history: "deep";
604
+ } ? never : Key;
605
+ }[HistoryStateKey<States>] extends never ? false : true;
606
+ type InitializerClosureForNode<AllStates extends Machine.StateSchemas, Node, Path extends string> = Node extends {
607
+ readonly type: "parallel";
608
+ readonly states: infer Children extends Machine.StateSchemas;
609
+ } ? Extract<Path, Machine.StateIdentifier<AllStates>> | InitializerClosuresForChildren<AllStates, Children, Path> : Node extends {
610
+ readonly states: infer Children extends Machine.StateSchemas;
611
+ readonly initial: infer Initial;
612
+ } ? Extract<Path, Machine.StateIdentifier<AllStates>> | (Initial extends ActiveStateKey<Children> ? InitializerClosureForNode<AllStates, Children[Initial], Machine.JoinPath<Path, Initial>> : never) : never;
613
+ type InitializerClosuresForChildren<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, Prefix extends string> = {
614
+ readonly [Key in ActiveStateKey<States>]: InitializerClosureForNode<AllStates, States[Key], Machine.JoinPath<Prefix, Key>>;
615
+ }[ActiveStateKey<States>];
616
+ type RequiredHistoryInitializersWithPrefix<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, Prefix extends string> = {
617
+ readonly [Key in ActiveStateKey<States>]: States[Key] extends {
618
+ readonly states: infer Children extends Machine.StateSchemas;
619
+ } ? (HasDirectShallowHistory<Children> extends true ? InitializerClosuresForChildren<AllStates, Children, Machine.JoinPath<Prefix, Key>> : never) | RequiredHistoryInitializersWithPrefix<AllStates, Children, Machine.JoinPath<Prefix, Key>> : never;
620
+ }[ActiveStateKey<States>];
494
621
  type SpawnRequirements<Requirements> = Exclude<Requirements, Scope.Scope>;
495
622
  type SpawnIdError<Options extends SpawnOptions> = "id" extends keyof Options ? Options extends {
496
623
  readonly id?: infer Id;
@@ -743,7 +870,7 @@ export declare namespace ChildMachine {
743
870
  * @category utility types
744
871
  * @since 4.0.0
745
872
  */
746
- type Ref<Child> = Child extends ChildMachine<string, infer M> ? M extends Machine<infer States, infer Events, any, any, infer E, infer R, infer InitialE, infer InitialR, any, infer Output, any, any, infer InputEvents> ? MachineRef<Machine.Snapshot<States>, Machine.EventOf<InputEvents>, E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError, Output> : never : never;
873
+ type Ref<Child> = Child extends ChildMachine<string, infer M> ? MachineRef<Machine.Snapshot<Machine.States<M>>, Machine.InputEvent<M>, Machine.Error<M> | ActionError<Machine.Services<M>> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError, Machine.Output<M>> : never;
747
874
  /**
748
875
  * Event accepted by the child selected by a descriptor.
749
876
  *
@@ -833,6 +960,26 @@ export interface SpawnIdOptions extends SpawnOptions {
833
960
  * @since 4.0.0
834
961
  */
835
962
  export declare namespace Machine {
963
+ /**
964
+ * Stable type-level representation carried by every machine definition.
965
+ *
966
+ * @internal
967
+ */
968
+ interface TypeCarrier<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Input extends Schema.Top, UnhandledStates extends StateIdentifier<States>, E, R, InitialE, InitialR, FinalStates extends StateIdentifier<States>, Output, Emits extends ReadonlyArray<TaggedSchema>, OutputStates extends StateIdentifier<States>, InputEvents extends ReadonlyArray<TaggedSchema>> {
969
+ readonly states: States;
970
+ readonly events: Events;
971
+ readonly input: Input;
972
+ readonly unhandledStates: UnhandledStates;
973
+ readonly error: E;
974
+ readonly services: R;
975
+ readonly initialError: InitialE;
976
+ readonly initialServices: InitialR;
977
+ readonly finalStates: FinalStates;
978
+ readonly output: Output;
979
+ readonly emits: Emits;
980
+ readonly outputStates: OutputStates;
981
+ readonly inputEvents: InputEvents;
982
+ }
836
983
  /**
837
984
  * Any schema-first machine.
838
985
  *
@@ -847,6 +994,8 @@ export declare namespace Machine {
847
994
  */
848
995
  interface Any extends Pipeable {
849
996
  readonly [TypeId]: TypeId;
997
+ /** @internal */
998
+ readonly [MachineTypeId]: TypeCarrier<any, any, any, any, any, any, any, any, any, any, any, any, any>;
850
999
  readonly states: StateSchemas;
851
1000
  readonly events: ReadonlyArray<TaggedSchema>;
852
1001
  readonly internalEvents: ReadonlyArray<TaggedSchema>;
@@ -863,20 +1012,118 @@ export declare namespace Machine {
863
1012
  /** @internal */
864
1013
  readonly initial: any;
865
1014
  }
1015
+ /**
1016
+ * Extracts the state schema tree carried by a machine definition.
1017
+ *
1018
+ * @category utility types
1019
+ * @since 4.0.0
1020
+ */
1021
+ type States<M extends Any> = M[typeof MachineTypeId]["states"];
1022
+ /**
1023
+ * Extracts the complete event schema tuple carried by a machine definition.
1024
+ *
1025
+ * @category utility types
1026
+ * @since 4.0.0
1027
+ */
1028
+ type Events<M extends Any> = M[typeof MachineTypeId]["events"];
1029
+ /**
1030
+ * Extracts the input schema carried by a machine definition.
1031
+ *
1032
+ * @category utility types
1033
+ * @since 4.0.0
1034
+ */
1035
+ type Input<M extends Any> = M[typeof MachineTypeId]["input"];
1036
+ /**
1037
+ * Extracts state paths that do not yet have handlers.
1038
+ *
1039
+ * @category utility types
1040
+ * @since 4.0.0
1041
+ */
1042
+ type UnhandledStates<M extends Any> = M[typeof MachineTypeId]["unhandledStates"];
1043
+ /**
1044
+ * Extracts the runtime error channel carried by a machine definition.
1045
+ *
1046
+ * @category utility types
1047
+ * @since 4.0.0
1048
+ */
1049
+ type Error<M extends Any> = M[typeof MachineTypeId]["error"];
1050
+ /**
1051
+ * Extracts runtime service requirements carried by a machine definition.
1052
+ *
1053
+ * @category utility types
1054
+ * @since 4.0.0
1055
+ */
1056
+ type Services<M extends Any> = M[typeof MachineTypeId]["services"];
1057
+ /**
1058
+ * Extracts the startup error channel carried by a machine definition.
1059
+ *
1060
+ * @category utility types
1061
+ * @since 4.0.0
1062
+ */
1063
+ type InitialError<M extends Any> = M[typeof MachineTypeId]["initialError"];
1064
+ /**
1065
+ * Extracts startup service requirements carried by a machine definition.
1066
+ *
1067
+ * @category utility types
1068
+ * @since 4.0.0
1069
+ */
1070
+ type InitialServices<M extends Any> = M[typeof MachineTypeId]["initialServices"];
1071
+ /**
1072
+ * Extracts final state paths carried by a machine definition.
1073
+ *
1074
+ * @category utility types
1075
+ * @since 4.0.0
1076
+ */
1077
+ type FinalStates<M extends Any> = M[typeof MachineTypeId]["finalStates"];
1078
+ /**
1079
+ * Extracts the terminal output channel carried by a machine definition.
1080
+ *
1081
+ * @category utility types
1082
+ * @since 4.0.0
1083
+ */
1084
+ type Output<M extends Any> = M[typeof MachineTypeId]["output"];
1085
+ /**
1086
+ * Extracts the emitted event schema tuple carried by a machine definition.
1087
+ *
1088
+ * @category utility types
1089
+ * @since 4.0.0
1090
+ */
1091
+ type Emits<M extends Any> = M[typeof MachineTypeId]["emits"];
1092
+ /**
1093
+ * Extracts state paths with implemented output handlers.
1094
+ *
1095
+ * @category utility types
1096
+ * @since 4.0.0
1097
+ */
1098
+ type OutputStates<M extends Any> = M[typeof MachineTypeId]["outputStates"];
1099
+ /**
1100
+ * Extracts the public input event schema tuple carried by a machine definition.
1101
+ *
1102
+ * @category utility types
1103
+ * @since 4.0.0
1104
+ */
1105
+ type InputEvents<M extends Any> = M[typeof MachineTypeId]["inputEvents"];
866
1106
  /**
867
1107
  * Extracts the complete event protocol handled inside a machine.
868
1108
  *
869
1109
  * @category utility types
870
1110
  * @since 4.0.0
871
1111
  */
872
- type Event<M extends Any> = M extends Machine<any, infer Events, any, any, any, any, any, any, any, any, any, any, any> ? EventOf<Events> : never;
1112
+ type Event<M extends Any> = EventOf<Events<M>>;
873
1113
  /**
874
1114
  * Extracts the event protocol accepted by public machine input boundaries.
875
1115
  *
876
1116
  * @category utility types
877
1117
  * @since 4.0.0
878
1118
  */
879
- type InputEvent<M extends Any> = M extends Machine<any, any, any, any, any, any, any, any, any, any, any, any, infer InputEvents> ? EventOf<InputEvents> : never;
1119
+ type InputEvent<M extends Any> = EventOf<InputEvents<M>>;
1120
+ /**
1121
+ * Extracts the event protocol emitted by a machine.
1122
+ *
1123
+ * @category utility types
1124
+ * @since 4.0.0
1125
+ */
1126
+ type Emit<M extends Any> = EmitOf<Emits<M>>;
880
1127
  /**
881
1128
  * A schema whose decoded value contains a `_tag` discriminator.
882
1129
  *
@@ -931,13 +1178,27 @@ export declare namespace Machine {
931
1178
  readonly output?: Schema.Top;
932
1179
  readonly states: StateTree;
933
1180
  }
1181
+ /**
1182
+ * Pseudo-state that restores the last active configuration of its parent.
1183
+ *
1184
+ * History nodes are transition targets only. They never become active and
1185
+ * therefore do not declare a state value schema or lifecycle handlers.
1186
+ *
1187
+ * @category models
1188
+ * @since 4.0.0
1189
+ */
1190
+ interface HistoryStateNodeConfig {
1191
+ readonly type: "history";
1192
+ /** Defaults to shallow history. */
1193
+ readonly history?: "shallow" | "deep";
1194
+ }
934
1195
  /**
935
1196
  * Configuration accepted for an object state node.
936
1197
  *
937
1198
  * @category models
938
1199
  * @since 4.0.0
939
1200
  */
940
- type StateNodeConfig = AtomicStateNodeConfig | CompoundStateNodeConfig | ParallelStateNodeConfig;
1201
+ type StateNodeConfig = AtomicStateNodeConfig | CompoundStateNodeConfig | ParallelStateNodeConfig | HistoryStateNodeConfig;
941
1202
  /**
942
1203
  * Object state tree keyed by state path.
943
1204
  *
@@ -1032,9 +1293,10 @@ export declare namespace Machine {
1032
1293
  interface StateNode {
1033
1294
  readonly path: string;
1034
1295
  readonly key: string;
1035
- readonly schema: TaggedSchema;
1296
+ readonly schema: TaggedSchema | undefined;
1036
1297
  readonly output: Schema.Top | undefined;
1037
- readonly type: "atomic" | "compound" | "parallel" | "final";
1298
+ readonly type: "atomic" | "compound" | "parallel" | "final" | "history";
1299
+ readonly history: "shallow" | "deep" | undefined;
1038
1300
  readonly parent: string | undefined;
1039
1301
  readonly children: ReadonlyArray<string>;
1040
1302
  readonly initial: string | undefined;
@@ -1095,10 +1357,47 @@ export declare namespace Machine {
1095
1357
  * @since 4.0.0
1096
1358
  */
1097
1359
  type StateIdentifierWithPrefix<States extends StateSchemas, Prefix extends string = ""> = {
1098
- readonly [Key in Extract<keyof States, string>]: States[Key] extends {
1360
+ readonly [Key in Extract<keyof States, string>]: States[Key] extends HistoryStateNodeConfig ? never : States[Key] extends {
1099
1361
  readonly states: infer Children;
1100
1362
  } ? Children extends StateSchemas ? JoinPath<Prefix, Key> | StateIdentifierWithPrefix<Children, JoinPath<Prefix, Key>> : JoinPath<Prefix, Key> : JoinPath<Prefix, Key>;
1101
1363
  }[Extract<keyof States, string>];
1364
+ /**
1365
+ * Extracts the transition-only history pseudo-state paths in a definition.
1366
+ *
1367
+ * @category utility types
1368
+ * @since 4.0.0
1369
+ */
1370
+ type HistoryIdentifier<States extends StateSchemas> = HistoryIdentifierWithPrefix<States>;
1371
+ /** @internal */
1372
+ type HistoryIdentifierWithPrefix<States extends StateSchemas, Prefix extends string = ""> = {
1373
+ readonly [Key in Extract<keyof States, string>]: States[Key] extends HistoryStateNodeConfig ? JoinPath<Prefix, Key> : States[Key] extends {
1374
+ readonly states: infer Children extends StateSchemas;
1375
+ } ? HistoryIdentifierWithPrefix<Children, JoinPath<Prefix, Key>> : never;
1376
+ }[Extract<keyof States, string>];
1377
+ /** Active keys directly declared in a state tree. */
1378
+ type ActiveStateKey<States extends StateSchemas> = {
1379
+ readonly [Key in Extract<keyof States, string>]: States[Key] extends HistoryStateNodeConfig ? never : Key;
1380
+ }[Extract<keyof States, string>];
1381
+ /** History pseudo-state keys directly declared in a state tree. */
1382
+ type HistoryStateKey<States extends StateSchemas> = {
1383
+ readonly [Key in Extract<keyof States, string>]: States[Key] extends HistoryStateNodeConfig ? Key : never;
1384
+ }[Extract<keyof States, string>];
1385
+ /**
1386
+ * Active states that must implement implicit initial-value construction for
1387
+ * shallow history restoration.
1388
+ *
1389
+ * @category utility types
1390
+ * @since 4.0.0
1391
+ */
1392
+ type RequiredHistoryInitializers<States extends StateSchemas> = [HistoryIdentifier<States>] extends [never] ? never : Extract<RequiredHistoryInitializersWithPrefix<States, States, "">, StateIdentifier<States>>;
1393
+ /** Active parent states that own one or more history pseudo-states. */
1394
+ type HistoryParentIdentifier<States extends StateSchemas> = HistoryIdentifier<States> extends infer HistoryId ? HistoryId extends string ? Extract<ImmediateParentStateIdentifier<HistoryId>, StateIdentifier<States>> : never : never;
1395
+ /** History defaults and implicit initializers that remain unimplemented. */
1396
+ type MissingHistoryImplementations<States extends StateSchemas, UnhandledStates extends StateIdentifier<States>> = Extract<UnhandledStates, HistoryParentIdentifier<States> | RequiredHistoryInitializers<States>>;
1397
+ /** @internal Readiness proof required by planning and managed execution. */
1398
+ type EnsureHistoryImplementations<States extends StateSchemas, UnhandledStates extends StateIdentifier<States>> = [HistoryIdentifier<States>] extends [never] ? unknown : [MissingHistoryImplementations<States, UnhandledStates>] extends [never] ? unknown : {
1399
+ readonly "~effect/Machine/MissingHistoryImplementation": MissingHistoryImplementations<States, UnhandledStates>;
1400
+ };
1102
1401
  /**
1103
1402
  * Extracts a state-tree node by state path.
1104
1403
  *
@@ -1214,8 +1513,8 @@ export declare namespace Machine {
1214
1513
  } ? OutputByIdentifier<States, StateId> : never;
1215
1514
  type CompoundCompletionOutput<States extends StateSchemas, Children extends StateSchemas, Prefix extends StateIdentifier<States>> = UndefinedIfNever<CompoundCompletionOutputRaw<States, Children, Prefix>>;
1216
1515
  type CompoundCompletionOutputRaw<States extends StateSchemas, Children extends StateSchemas, Prefix extends StateIdentifier<States>> = {
1217
- readonly [Key in Extract<keyof Children, string>]: DirectFinalCompletionOutput<States, Extract<JoinPath<Prefix, Key>, StateIdentifier<States>>>;
1218
- }[Extract<keyof Children, string>];
1516
+ readonly [Key in ActiveStateKey<Children>]: DirectFinalCompletionOutput<States, Extract<JoinPath<Prefix, Key>, StateIdentifier<States>>>;
1517
+ }[ActiveStateKey<Children>];
1219
1518
  /**
1220
1519
  * Extracts the output passed when a state node completes.
1221
1520
  *
@@ -1317,6 +1616,12 @@ export declare namespace Machine {
1317
1616
  readonly path: string;
1318
1617
  readonly output?: unknown;
1319
1618
  }
1619
+ /** Encoded values and paths retained by one history pseudo-state. */
1620
+ interface EncodedSnapshotHistoryEntry {
1621
+ readonly mode: "shallow" | "deep";
1622
+ readonly active: ReadonlyArray<string>;
1623
+ readonly values: Readonly<Record<string, unknown>>;
1624
+ }
1320
1625
  /**
1321
1626
  * Normalized data representation of a machine snapshot.
1322
1627
  *
@@ -1333,6 +1638,7 @@ export declare namespace Machine {
1333
1638
  readonly _tag: "MachineSnapshot";
1334
1639
  readonly active: ReadonlyArray<EncodedSnapshotState>;
1335
1640
  readonly completed?: ReadonlyArray<EncodedSnapshotCompletion>;
1641
+ readonly history?: Readonly<Record<string, EncodedSnapshotHistoryEntry>>;
1336
1642
  }
1337
1643
  /**
1338
1644
  * Completed state path and its resolved output value.
@@ -1344,6 +1650,12 @@ export declare namespace Machine {
1344
1650
  readonly path: string;
1345
1651
  readonly output: unknown;
1346
1652
  }
1653
+ /** Decoded values and paths retained by one history pseudo-state. */
1654
+ interface SnapshotHistoryEntry {
1655
+ readonly mode: "shallow" | "deep";
1656
+ readonly active: ReadonlyArray<string>;
1657
+ readonly values: Readonly<Record<string, unknown>>;
1658
+ }
1347
1659
  /**
1348
1660
  * Carries lifecycle metadata required to resume planning from a cloned
1349
1661
  * snapshot.
@@ -1362,6 +1674,7 @@ export declare namespace Machine {
1362
1674
  */
1363
1675
  interface SnapshotMetadata {
1364
1676
  readonly completed?: ReadonlyArray<SnapshotCompletion>;
1677
+ readonly history?: Readonly<Record<string, SnapshotHistoryEntry>>;
1365
1678
  }
1366
1679
  /**
1367
1680
  * Atomic statechart snapshot carrying path identity separately from the
@@ -1418,8 +1731,8 @@ export declare namespace Machine {
1418
1731
  * @since 4.0.0
1419
1732
  */
1420
1733
  type SnapshotWithPrefix<States extends StateSchemas, Prefix extends string> = {
1421
- readonly [Key in Extract<keyof States, string>]: SnapshotByIdentifierWithPath<States, Key, JoinPath<Prefix, Key>>;
1422
- }[Extract<keyof States, string>];
1734
+ readonly [Key in ActiveStateKey<States>]: SnapshotByIdentifierWithPath<States, Key, JoinPath<Prefix, Key>>;
1735
+ }[ActiveStateKey<States>];
1423
1736
  /**
1424
1737
  * Extracts child snapshots under a parallel parent path prefix, keyed by
1425
1738
  * child region.
@@ -1428,7 +1741,7 @@ export declare namespace Machine {
1428
1741
  * @since 4.0.0
1429
1742
  */
1430
1743
  type SnapshotRegionsWithPrefix<States extends StateSchemas, Prefix extends string> = {
1431
- readonly [Key in Extract<keyof States, string>]: SnapshotByIdentifierWithPath<States, Key, JoinPath<Prefix, Key>>;
1744
+ readonly [Key in ActiveStateKey<States>]: SnapshotByIdentifierWithPath<States, Key, JoinPath<Prefix, Key>>;
1432
1745
  };
1433
1746
  /**
1434
1747
  * Extracts a snapshot for a state node while preserving its full path.
@@ -1436,7 +1749,7 @@ export declare namespace Machine {
1436
1749
  * @category utility types
1437
1750
  * @since 4.0.0
1438
1751
  */
1439
- type SnapshotByIdentifierWithPath<States extends StateSchemas, StateId extends Extract<keyof States, string>, Path extends string> = States[StateId] extends {
1752
+ type SnapshotByIdentifierWithPath<States extends StateSchemas, StateId extends ActiveStateKey<States>, Path extends string> = States[StateId] extends {
1440
1753
  readonly type: "parallel";
1441
1754
  readonly states: infer Children;
1442
1755
  } ? Children extends StateSchemas ? ParallelSnapshot<Path, NodeSchema<States[StateId]>["Type"], SnapshotRegionsWithPrefix<Children, Path>> : AtomicSnapshot<Path, NodeSchema<States[StateId]>["Type"]> : States[StateId] extends {
@@ -1450,8 +1763,8 @@ export declare namespace Machine {
1450
1763
  * @since 4.0.0
1451
1764
  */
1452
1765
  type Snapshot<States extends StateSchemas> = {
1453
- readonly [StateId in Extract<keyof States, string>]: SnapshotByIdentifier<States, StateId & StateIdentifier<States>>;
1454
- }[Extract<keyof States, string>];
1766
+ readonly [StateId in ActiveStateKey<States>]: SnapshotByIdentifier<States, StateId & StateIdentifier<States>>;
1767
+ }[ActiveStateKey<States>];
1455
1768
  /**
1456
1769
  * Extracts the root state identifier from a state path.
1457
1770
  *
@@ -1486,6 +1799,19 @@ export declare namespace Machine {
1486
1799
  type EventByTag<Events extends ReadonlyArray<TaggedSchema>, Tag extends TagOf<Events[number]>> = Extract<EventOf<Events>, {
1487
1800
  readonly _tag: Tag;
1488
1801
  }>;
1802
+ /**
1803
+ * Opaque state construction returned by a builder's `.from` method.
1804
+ *
1805
+ * The machine resolves the instruction through the selected state schema
1806
+ * while planning. Its decoded value is intentionally unavailable until
1807
+ * planning succeeds.
1808
+ *
1809
+ * @category models
1810
+ * @since 4.0.0
1811
+ */
1812
+ interface StateConstruction<Result> {
1813
+ readonly [Model.StateConstructionTypeId]: Result;
1814
+ }
1489
1815
  /**
1490
1816
  * Machine-bound target instruction accepted from transition handlers.
1491
1817
  *
@@ -1494,12 +1820,30 @@ export declare namespace Machine {
1494
1820
  */
1495
1821
  interface Target<States extends StateSchemas, StateId extends StateIdentifier<States>> {
1496
1822
  readonly [Model.TargetTypeId]: typeof Model.TargetTypeId;
1823
+ readonly [Model.TargetSnapshotTypeId]?: SnapshotByIdentifier<States, StateId>;
1497
1824
  readonly path: StateId;
1498
1825
  readonly value: StateByIdentifier<States, StateId>;
1499
1826
  readonly values?: Partial<{
1500
1827
  readonly [AncestorStateId in StateIdentifier<States>]: StateByIdentifier<States, AncestorStateId>;
1501
1828
  }>;
1502
1829
  }
1830
+ /**
1831
+ * Transition instruction that restores a history pseudo-state's parent.
1832
+ *
1833
+ * Unlike ordinary targets, history targets carry no state value. The
1834
+ * planner resolves the remembered concrete configuration, or evaluates the
1835
+ * history node's typed default when no record exists.
1836
+ *
1837
+ * @category models
1838
+ * @since 4.0.0
1839
+ */
1840
+ interface HistoryTarget<States extends StateSchemas, HistoryId extends HistoryIdentifier<States>> {
1841
+ readonly [Model.HistoryTargetTypeId]: typeof Model.HistoryTargetTypeId;
1842
+ readonly path: HistoryId;
1843
+ readonly parent: Extract<ParentPath<HistoryId>, StateIdentifier<States>>;
1844
+ }
1845
+ /** Builder containing only history pseudo-state paths. */
1846
+ type HistoryTargetBuilder<States extends StateSchemas> = HistoryTargetBuilderWithPrefix<States, States, "">;
1503
1847
  /**
1504
1848
  * Builder for complete transition snapshots.
1505
1849
  *
@@ -1523,7 +1867,7 @@ export declare namespace Machine {
1523
1867
  * @category utility types
1524
1868
  * @since 4.0.0
1525
1869
  */
1526
- type LocalTargetBuilder<States extends StateSchemas, Source extends StateIdentifier<States>> = NearestCompoundScope<States, Source> extends infer Scope ? [Scope] extends [never] ? {} : Scope extends StateIdentifier<States> ? LocalTargetBuilderForScope<States, Scope> : {} : {};
1870
+ type LocalTargetBuilder<States extends StateSchemas, Source extends StateIdentifier<States>> = NearestCompoundScope<States, Source> extends infer Scope ? [Scope] extends [never] ? {} : Scope extends StateIdentifier<States> ? LocalTargetBuilderForScope<States, Scope, Source> : {} : {};
1527
1871
  /**
1528
1872
  * Builder for partial transition targets within the active source root.
1529
1873
  *
@@ -1535,7 +1879,7 @@ export declare namespace Machine {
1535
1879
  * @category utility types
1536
1880
  * @since 4.0.0
1537
1881
  */
1538
- type BranchTargetBuilder<States extends StateSchemas, Source extends StateIdentifier<States>> = BranchTargetBuilderForRoot<States, Extract<RootStateIdentifier<Source>, Extract<keyof States, string>>>;
1882
+ type BranchTargetBuilder<States extends StateSchemas, Source extends StateIdentifier<States>> = BranchTargetBuilderForRoot<States, Extract<RootStateIdentifier<Source>, ActiveStateKey<States>>, Source>;
1539
1883
  /**
1540
1884
  * Machine-bound target builders available in transition contexts.
1541
1885
  *
@@ -1584,6 +1928,8 @@ export declare namespace Machine {
1584
1928
  * @since 4.0.0
1585
1929
  */
1586
1930
  readonly full: FullTargetBuilder<States>;
1931
+ /** Restores a declared shallow or deep history pseudo-state. */
1932
+ readonly history: HistoryTargetBuilder<States>;
1587
1933
  }
1588
1934
  /**
1589
1935
  * Stages an Effect to run after the current machine step is planned.
@@ -1742,7 +2088,7 @@ export declare namespace Machine {
1742
2088
  readonly type: "parallel";
1743
2089
  readonly states: infer Children extends StateSchemas;
1744
2090
  } ? {
1745
- readonly [Key in Extract<keyof Children, string>]: CompletionOutputByIdentifier<States, Extract<JoinPath<StateId, Key>, StateIdentifier<States>>>;
2091
+ readonly [Key in ActiveStateKey<Children>]: CompletionOutputByIdentifier<States, Extract<JoinPath<StateId, Key>, StateIdentifier<States>>>;
1746
2092
  } : never;
1747
2093
  /**
1748
2094
  * Context passed to a parallel state output function.
@@ -1770,7 +2116,7 @@ export declare namespace Machine {
1770
2116
  * @category utility types
1771
2117
  * @since 4.0.0
1772
2118
  */
1773
- type InitialResult<States extends StateSchemas, E, R> = Snapshot<States> | Effect.Effect<Snapshot<States>, E, R>;
2119
+ type InitialResult<States extends StateSchemas, E, R> = Snapshot<States> | StateConstruction<Snapshot<States>> | Effect.Effect<Snapshot<States> | StateConstruction<Snapshot<States>>, E, R>;
1774
2120
  /**
1775
2121
  * Return value accepted from transition handlers.
1776
2122
  *
@@ -1783,7 +2129,7 @@ export declare namespace Machine {
1783
2129
  * @category utility types
1784
2130
  * @since 4.0.0
1785
2131
  */
1786
- type HandlerResult<States extends StateSchemas, E, R> = Snapshot<States> | Target<States, StateIdentifier<States>> | void | Effect.Effect<Snapshot<States> | Target<States, StateIdentifier<States>> | void, E, R>;
2132
+ type HandlerResult<States extends StateSchemas, E, R> = Snapshot<States> | Target<States, StateIdentifier<States>> | HistoryTarget<States, HistoryIdentifier<States>> | StateConstruction<Snapshot<States> | Target<States, StateIdentifier<States>> | HistoryTarget<States, HistoryIdentifier<States>>> | void | Effect.Effect<Snapshot<States> | Target<States, StateIdentifier<States>> | HistoryTarget<States, HistoryIdentifier<States>> | StateConstruction<Snapshot<States> | Target<States, StateIdentifier<States>> | HistoryTarget<States, HistoryIdentifier<States>>> | void, E, R>;
1787
2133
  /**
1788
2134
  * Extracts the union of handler return values from a handler map.
1789
2135
  *
@@ -1819,6 +2165,18 @@ export declare namespace Machine {
1819
2165
  * @since 4.0.0
1820
2166
  */
1821
2167
  type StateActionReturn<Config, Key extends "entry" | "exit"> = Key extends keyof Config ? NonNullable<Config[Key]> extends (...args: any) => infer Ret ? Ret : never : never;
2168
+ /** Extracts the return value from an implicit initial child implementation. */
2169
+ type StateInitialReturn<Config> = Config extends {
2170
+ readonly initial?: infer Initial;
2171
+ } ? NonNullable<Initial> extends (...args: any) => infer Ret ? Ret : never : never;
2172
+ /** Extracts the return values from a state's history defaults. */
2173
+ type HistoryDefaultReturn<Config> = Config extends {
2174
+ readonly history?: infer History;
2175
+ } ? {
2176
+ readonly [Key in keyof NonNullable<History>]: NonNullable<History>[Key] extends {
2177
+ readonly default: (...args: any) => infer Ret;
2178
+ } ? Ret : never;
2179
+ }[keyof NonNullable<History>] : never;
1822
2180
  /**
1823
2181
  * Extracts the return value from an event transition config.
1824
2182
  *
@@ -1970,7 +2328,7 @@ export declare namespace Machine {
1970
2328
  * @category utility types
1971
2329
  * @since 4.0.0
1972
2330
  */
1973
- type ConfigServices<Config> = Effect.Services<EventHandlerReturn<Config>> | Effect.Services<AlwaysReturn<Config>> | Effect.Services<DoneReturn<Config>> | Effect.Services<StateActionReturn<Config, "entry">> | Effect.Services<StateActionReturn<Config, "exit">> | InvokeRequirements<Config>;
2331
+ type ConfigServices<Config> = Effect.Services<EventHandlerReturn<Config>> | Effect.Services<AlwaysReturn<Config>> | Effect.Services<DoneReturn<Config>> | Effect.Services<StateActionReturn<Config, "entry">> | Effect.Services<StateActionReturn<Config, "exit">> | Effect.Services<StateInitialReturn<Config>> | Effect.Services<HistoryDefaultReturn<Config>> | InvokeRequirements<Config>;
1974
2332
  /**
1975
2333
  * Configuration for invoking a child process while a state is active.
1976
2334
  *
@@ -2011,6 +2369,9 @@ export declare namespace Machine {
2011
2369
  readonly initialError: Types.Covariant<InitialError>;
2012
2370
  };
2013
2371
  }
2372
+ interface InvokeDefinitionValue {
2373
+ readonly [InvokeTypeId]: unknown;
2374
+ }
2014
2375
  /**
2015
2376
  * State-bound configuration for invoked child processes.
2016
2377
  *
@@ -2022,7 +2383,7 @@ export declare namespace Machine {
2022
2383
  * @category models
2023
2384
  * @since 4.0.0
2024
2385
  */
2025
- type InvokeDefinition<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<States>> = AnyInvokeConfig<any, any, any, any, any, any> | ReadonlyArray<AnyInvokeConfig<any, any, any, any, any, any>> | ((context: InvokeContext<States, Events, Emits, StateId>) => AnyInvokeConfig<any, any, any, any, any, any> | ReadonlyArray<AnyInvokeConfig<any, any, any, any, any, any>>);
2386
+ type InvokeDefinition<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<States>> = InvokeDefinitionValue | ReadonlyArray<InvokeDefinitionValue> | ((context: InvokeContext<States, Events, Emits, StateId>) => InvokeDefinitionValue | ReadonlyArray<InvokeDefinitionValue>);
2026
2387
  type OutputHandlerConfig<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<States>, Context> = NodeByIdentifier<States, StateId> extends {
2027
2388
  readonly output: Schema.Top;
2028
2389
  } ? {
@@ -2053,7 +2414,37 @@ export declare namespace Machine {
2053
2414
  readonly transition: (context: HandlerContext<States, Events, Emits, StateId, EventTag, E, R>) => HandlerResult<States, any, any>;
2054
2415
  };
2055
2416
  };
2417
+ readonly initial?: StateInitialHandler<States, Events, Emits, StateId>;
2056
2418
  } & ActiveOutputHandlerConfig<States, Events, StateId>;
2419
+ /** Values supplied when a statechart implicitly enters a state's initial children. */
2420
+ type StateInitialValue<States extends StateSchemas, StateId extends StateIdentifier<States>> = NodeByIdentifier<States, StateId> extends infer Node ? Node extends {
2421
+ readonly type: "parallel";
2422
+ readonly states: infer Children extends StateSchemas;
2423
+ } ? {
2424
+ readonly [Key in ActiveStateKey<Children>]: NodeSchema<Children[Key]>["Type"];
2425
+ } : Node extends {
2426
+ readonly states: infer Children extends StateSchemas;
2427
+ readonly initial: infer Initial;
2428
+ } ? Initial extends ActiveStateKey<Children> ? NodeSchema<Children[Initial]>["Type"] : never : never : never;
2429
+ /** Context passed to an implicit child-state initializer. */
2430
+ type StateInitialContext<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<States>> = StateActionContext<States, Events, Emits, StateId>;
2431
+ /** Initial child value implementation for a compound or parallel state. */
2432
+ type StateInitialHandler<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<States>> = (context: StateInitialContext<States, Events, Emits, StateId>) => StateInitialValue<States, StateId> | Effect.Effect<StateInitialValue<States, StateId>, any, any>;
2433
+ /** Context used only when a history node has no previously captured record. */
2434
+ interface HistoryDefaultContext<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, ParentId extends StateIdentifier<States>> extends PlanningCapabilities<EventOf<Events>, EmitOf<Emits>> {
2435
+ readonly event: LifecycleEvent<Events>;
2436
+ readonly runtime: RuntimeEffect<Events, Emits>;
2437
+ readonly target: FullTargetBuilder<States>;
2438
+ readonly parent: ParentId;
2439
+ }
2440
+ /** Typed fallback evaluated when a history node has no record yet. */
2441
+ type HistoryDefaultHandler<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, ParentId extends StateIdentifier<States>> = (context: HistoryDefaultContext<States, Events, Emits, ParentId>) => SnapshotByIdentifier<States, ParentId> | StateConstruction<SnapshotByIdentifier<States, ParentId>> | Effect.Effect<SnapshotByIdentifier<States, ParentId> | StateConstruction<SnapshotByIdentifier<States, ParentId>>, any, any>;
2442
+ /** Default implementations keyed by direct history child. */
2443
+ type HistoryDefaultConfig<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, ParentId extends StateIdentifier<States>, Children extends StateSchemas> = {
2444
+ readonly [Key in HistoryStateKey<Children>]?: {
2445
+ readonly default: HistoryDefaultHandler<States, Events, Emits, ParentId>;
2446
+ };
2447
+ };
2057
2448
  /**
2058
2449
  * Configuration accepted for a final state.
2059
2450
  *
@@ -2085,19 +2476,21 @@ export declare namespace Machine {
2085
2476
  };
2086
2477
  type HandlerNode<AllStates extends StateSchemas, Node, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, E, R, StateId extends StateIdentifier<AllStates>> = HandlerConfig<AllStates, Events, Emits, StateId, E, R> & (HandlerChildren<Node> extends infer Children extends StateSchemas ? [Children] extends [never] ? {
2087
2478
  readonly states?: never;
2479
+ readonly history?: never;
2088
2480
  } : {
2089
2481
  readonly states?: HandlerTree<AllStates, Children, Events, Emits, E, R, StateId>;
2482
+ readonly history?: HistoryDefaultConfig<AllStates, Events, Emits, StateId, Children>;
2090
2483
  } : {
2091
2484
  readonly states?: never;
2485
+ readonly history?: never;
2092
2486
  });
2093
2487
  type HandlerTree<AllStates extends StateSchemas, States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, E, R, Prefix extends string> = {
2094
- readonly [Key in Extract<keyof States, string>]?: HandlerNode<AllStates, States[Key], Events, Emits, E, R, HandlerStateId<AllStates, JoinPath<Prefix, Key>>>;
2488
+ readonly [Key in ActiveStateKey<States>]?: HandlerNode<AllStates, States[Key], Events, Emits, E, R, HandlerStateId<AllStates, JoinPath<Prefix, Key>>>;
2095
2489
  };
2096
- type HandlerNodeConfigKey = "always" | "entry" | "exit" | "invoke" | "on" | "onDone" | "output" | "states";
2097
- type HandlerValidationError<Message extends string> = {
2098
- readonly "~effect/Machine/HandlerError": Message;
2490
+ type HandlerNodeConfigKey = "always" | "entry" | "exit" | "history" | "initial" | "invoke" | "on" | "onDone" | "output" | "states";
2491
+ type HandlerValidationError<Message extends string, Path extends string, Detail = unknown> = {
2492
+ readonly "~effect/Machine/HandlerError": readonly [message: Message, path: Path, detail: Detail];
2099
2493
  };
2100
- type HandlerValidationErrors<Validation> = Validation extends HandlerValidationError<any> ? Validation : never;
2101
2494
  type NodeHasDeclaredOutput<States extends StateSchemas, StateId extends StateIdentifier<States>> = NodeByIdentifier<States, StateId> extends {
2102
2495
  readonly output: Schema.Top;
2103
2496
  } ? StateId : never;
@@ -2119,47 +2512,79 @@ export declare namespace Machine {
2119
2512
  readonly [Key in Extract<keyof Children, string>]: RequiredCompletionOutputStates<States, Extract<JoinPath<StateId, Key>, StateIdentifier<States>>>;
2120
2513
  }[Extract<keyof Children, string>] : never;
2121
2514
  type HandlerOutputStates<AllStates extends StateSchemas, StateId extends StateIdentifier<AllStates>, Config> = "output" extends keyof Config ? StateId : never;
2122
- type UnionToIntersection<Union> = (Union extends unknown ? (argument: Union) => void : never) extends (argument: infer Intersection) => void ? Intersection : never;
2123
- type HandlerUnknownStateKeyValidation<States extends StateSchemas, Config> = [Exclude<Extract<keyof Config, string>, Extract<keyof States, string>>] extends [never] ? unknown : HandlerValidationError<"Handler tree contains a state key that does not exist">;
2124
- type HandlerUnknownConfigKeyValidation<Config> = [
2125
- Exclude<Extract<keyof Config, string>, HandlerNodeConfigKey>
2126
- ] extends [never] ? unknown : HandlerValidationError<"Handler config contains an unknown key">;
2127
- type HandlerOnKeyValidation<Events extends ReadonlyArray<TaggedSchema>, Config> = Config extends {
2128
- readonly on?: infer On;
2129
- } ? [
2130
- Exclude<Extract<keyof NonNullable<On>, string>, TagOf<Events[number]>>
2131
- ] extends [never] ? unknown : HandlerValidationError<"Handler config contains an event key that does not exist"> : unknown;
2515
+ type HandlerUnknownStateKeyValidation<States extends StateSchemas, Prefix extends string, Config, UnknownKeys extends string = Exclude<Extract<keyof Config, string>, Extract<keyof States, string>>> = [UnknownKeys] extends [never] ? unknown : {
2516
+ readonly [Key in UnknownKeys]: HandlerValidationError<"Handler tree contains a state key that does not exist", JoinPath<Prefix, Key>, Key>;
2517
+ };
2518
+ type HandlerUnknownConfigKeyValidation<StateId extends string, Config, UnknownKeys extends string = Exclude<Extract<keyof Config, string>, HandlerNodeConfigKey>> = [UnknownKeys] extends [never] ? unknown : {
2519
+ readonly [Key in UnknownKeys]: HandlerValidationError<"Handler config contains an unknown key", StateId, Key>;
2520
+ };
2521
+ type HandlerOnKeyValidation<Events extends ReadonlyArray<TaggedSchema>, StateId extends string, Config, On = Config extends {
2522
+ readonly on?: infer Current;
2523
+ } ? NonNullable<Current> : never, UnknownKeys extends string = Exclude<Extract<keyof On, string>, TagOf<Events[number]>>> = "on" extends keyof Config ? [UnknownKeys] extends [never] ? unknown : {
2524
+ readonly on: {
2525
+ readonly [Key in UnknownKeys]: HandlerValidationError<"Handler config contains an event key that does not exist", StateId, Key>;
2526
+ };
2527
+ } : unknown;
2132
2528
  type HandlerDepth = readonly [unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown];
2133
2529
  type HandlerNextDepth<Depth extends ReadonlyArray<unknown>> = Depth extends readonly [unknown, ...infer Rest extends ReadonlyArray<unknown>] ? Rest : readonly [];
2134
- type HandlerChildrenValidation<AllStates extends StateSchemas, Node, Events extends ReadonlyArray<TaggedSchema>, Prefix extends string, Config, AvailableOutputStates extends StateIdentifier<AllStates>, Depth extends ReadonlyArray<unknown>> = "states" extends keyof Config ? Depth extends readonly [] ? HandlerValidationError<"Handler nesting exceeds the supported depth"> : Config extends {
2530
+ type HandlerChildrenValidation<AllStates extends StateSchemas, Node, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, Prefix extends string, Config, AvailableOutputStates extends StateIdentifier<AllStates>, Depth extends ReadonlyArray<unknown>> = "states" extends keyof Config ? Depth extends readonly [] ? {
2531
+ readonly states: HandlerValidationError<"Handler nesting exceeds the supported depth", Prefix>;
2532
+ } : Config extends {
2135
2533
  readonly states?: infer ChildrenConfig;
2136
- } ? HandlerChildren<Node> extends infer Children extends StateSchemas ? [
2137
- Children
2138
- ] extends [never] ? HandlerValidationError<"Handler config contains child states for a state that has no children"> : HandlerTreeValidation<AllStates, Children, Events, Prefix, NonNullable<ChildrenConfig>, AvailableOutputStates, HandlerNextDepth<Depth>> : HandlerValidationError<"Handler config contains child states for a state that has no children"> : unknown : unknown;
2534
+ } ? HandlerChildren<Node> extends infer Children extends StateSchemas ? [Children] extends [never] ? {
2535
+ readonly states: HandlerValidationError<"Handler config contains child states for a state that has no children", Prefix>;
2536
+ } : HandlerTreeValidation<AllStates, Children, Events, Emits, Prefix, NonNullable<ChildrenConfig>, AvailableOutputStates, HandlerNextDepth<Depth>> extends infer Validation ? unknown extends Validation ? unknown : {
2537
+ readonly states: Validation;
2538
+ } : never : {
2539
+ readonly states: HandlerValidationError<"Handler config contains child states for a state that has no children", Prefix>;
2540
+ } : unknown : unknown;
2139
2541
  type HandlerOutputRequirementValidation<AllStates extends StateSchemas, StateId extends StateIdentifier<AllStates>, AvailableOutputStates extends StateIdentifier<AllStates>, Config> = ("onDone" extends keyof Config ? [
2140
2542
  Exclude<RequiredCompletionOutputStates<AllStates, StateId>, AvailableOutputStates>
2141
- ] extends [never] ? unknown : HandlerValidationError<"Handler config is missing an output implementation required by onDone"> : unknown) & ("output" extends keyof Config ? NodeByIdentifier<AllStates, StateId> extends {
2543
+ ] extends [never] ? unknown : {
2544
+ readonly onDone: HandlerValidationError<"Handler config is missing an output implementation required by onDone", StateId, Exclude<RequiredCompletionOutputStates<AllStates, StateId>, AvailableOutputStates>>;
2545
+ } : unknown) & ("output" extends keyof Config ? NodeByIdentifier<AllStates, StateId> extends {
2142
2546
  readonly type: "parallel";
2143
2547
  } ? [
2144
2548
  Exclude<RequiredParallelOutputStates<AllStates, StateId>, AvailableOutputStates>
2145
- ] extends [never] ? unknown : HandlerValidationError<"Handler config is missing a region output implementation required by parallel output"> : unknown : unknown);
2146
- type HandlerNodeValidation<AllStates extends StateSchemas, Node, Events extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<AllStates>, Config, AvailableOutputStates extends StateIdentifier<AllStates>, Depth extends ReadonlyArray<unknown>> = HandlerUnknownConfigKeyValidation<Config> & HandlerOnKeyValidation<Events, Config> & HandlerInvokeOutputValidation<Events, Config> & HandlerInvokeEmitsValidation<Events, Config> & HandlerInvokeSnapshotValidation<Events, Config> & HandlerChildrenValidation<AllStates, Node, Events, StateId, Config, AvailableOutputStates, Depth> & HandlerOutputRequirementValidation<AllStates, StateId, AvailableOutputStates, Config>;
2147
- type HandlerInvokeOutputValidation<Events extends ReadonlyArray<TaggedSchema>, Config> = [InvokeReturn<Config>] extends [never] ? unknown : [Exclude<InvokeOutput<InvokeReturn<Config>>, EventOf<Events> | void>] extends [never] ? unknown : HandlerValidationError<"Invoked child output must be a machine event or void">;
2148
- type HandlerInvokeEmitsValidation<Events extends ReadonlyArray<TaggedSchema>, Config> = [InvokeReturn<Config>] extends [never] ? unknown : [Exclude<InvokeEmits<InvokeReturn<Config>>, EventOf<Events>>] extends [never] ? unknown : HandlerValidationError<"Invoked child emits events not accepted by the parent machine">;
2149
- type HandlerInvokeSnapshotValidation<Events extends ReadonlyArray<TaggedSchema>, Config> = [InvokeReturn<Config>] extends [never] ? unknown : IsAny<InvokeSnapshotEvent<InvokeReturn<Config>>> extends true ? unknown : [Exclude<InvokeSnapshotEvent<InvokeReturn<Config>>, EventOf<Events> | undefined>] extends [never] ? unknown : HandlerValidationError<"Invoked child snapshot mapper must return a machine event or undefined">;
2150
- type HandlerTreeNodeValidationErrors<AllStates extends StateSchemas, States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Prefix extends string, Config, AvailableOutputStates extends StateIdentifier<AllStates>, Depth extends ReadonlyArray<unknown>> = {
2151
- readonly [Key in Extract<Extract<keyof Config, string>, Extract<keyof States, string>>]: HandlerValidationErrors<HandlerNodeValidation<AllStates, States[Key], Events, HandlerStateId<AllStates, JoinPath<Prefix, Key>>, Config[Key], AvailableOutputStates, Depth>>;
2152
- }[Extract<Extract<keyof Config, string>, Extract<keyof States, string>>];
2153
- type HandlerTreeNodeValidations<AllStates extends StateSchemas, States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Prefix extends string, Config, AvailableOutputStates extends StateIdentifier<AllStates>, Depth extends ReadonlyArray<unknown>> = HandlerTreeNodeValidationErrors<AllStates, States, Events, Prefix, Config, AvailableOutputStates, Depth> extends infer Errors ? [Errors] extends [never] ? unknown : UnionToIntersection<Errors> : never;
2154
- type HandlerTreeValidation<AllStates extends StateSchemas, States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Prefix extends string, Config, AvailableOutputStates extends StateIdentifier<AllStates>, Depth extends ReadonlyArray<unknown> = HandlerDepth> = HandlerUnknownStateKeyValidation<States, Config> & HandlerTreeNodeValidations<AllStates, States, Events, Prefix, Config, AvailableOutputStates, Depth>;
2549
+ ] extends [never] ? unknown : {
2550
+ readonly output: HandlerValidationError<"Handler config is missing a region output implementation required by parallel output", StateId, Exclude<RequiredParallelOutputStates<AllStates, StateId>, AvailableOutputStates>>;
2551
+ } : unknown : unknown);
2552
+ type HandlerNodeValidation<AllStates extends StateSchemas, Node, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<AllStates>, Config, AvailableOutputStates extends StateIdentifier<AllStates>, Depth extends ReadonlyArray<unknown>> = HandlerUnknownConfigKeyValidation<StateId, Config> & HandlerOnKeyValidation<Events, StateId, Config> & HandlerInvokeOutputValidation<Events, StateId, Config> & HandlerInvokeEmitsValidation<Events, StateId, Config> & HandlerInvokeSnapshotValidation<Events, StateId, Config> & HandlerChildrenValidation<AllStates, Node, Events, Emits, StateId, Config, AvailableOutputStates, Depth> & HandlerOutputRequirementValidation<AllStates, StateId, AvailableOutputStates, Config> & HandlerRuntimeValidation<Events, Emits, StateId, Config>;
2553
+ type HandlerInvokeOutputValidation<Events extends ReadonlyArray<TaggedSchema>, StateId extends string, Config> = [InvokeReturn<Config>] extends [never] ? unknown : [Exclude<InvokeOutput<InvokeReturn<Config>>, EventOf<Events> | void>] extends [never] ? unknown : {
2554
+ readonly invoke: HandlerValidationError<"Invoked child output must be a machine event or void", StateId, Exclude<InvokeOutput<InvokeReturn<Config>>, EventOf<Events> | void>>;
2555
+ };
2556
+ type HandlerInvokeEmitsValidation<Events extends ReadonlyArray<TaggedSchema>, StateId extends string, Config> = [InvokeReturn<Config>] extends [never] ? unknown : [Exclude<InvokeEmits<InvokeReturn<Config>>, EventOf<Events>>] extends [never] ? unknown : {
2557
+ readonly invoke: HandlerValidationError<"Invoked child emits events not accepted by the parent machine", StateId, Exclude<InvokeEmits<InvokeReturn<Config>>, EventOf<Events>>>;
2558
+ };
2559
+ type HandlerInvokeSnapshotValidation<Events extends ReadonlyArray<TaggedSchema>, StateId extends string, Config> = [InvokeReturn<Config>] extends [never] ? unknown : IsAny<InvokeSnapshotEvent<InvokeReturn<Config>>> extends true ? unknown : [Exclude<InvokeSnapshotEvent<InvokeReturn<Config>>, EventOf<Events> | undefined>] extends [never] ? unknown : {
2560
+ readonly invoke: HandlerValidationError<"Invoked child snapshot mapper must return a machine event or undefined", StateId, Exclude<InvokeSnapshotEvent<InvokeReturn<Config>>, EventOf<Events> | undefined>>;
2561
+ };
2562
+ type HandlerRuntimeValidation<Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, StateId extends string, Config, Incompatible = IncompatibleRuntime<ConfigServices<HandlerConfigPart<Config>>, EventOf<Events>, EmitOf<Emits>>> = [Incompatible] extends [never] ? unknown : HandlerValidationError<"Handler config requires an incompatible machine runtime", StateId, Incompatible>;
2563
+ type HandlerTreeNodeValidationMap<AllStates extends StateSchemas, States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, Prefix extends string, Config, AvailableOutputStates extends StateIdentifier<AllStates>, Depth extends ReadonlyArray<unknown>> = {
2564
+ readonly [Key in Extract<Extract<keyof Config, string>, Extract<keyof States, string>>]?: HandlerNodeValidation<AllStates, States[Key], Events, Emits, HandlerStateId<AllStates, JoinPath<Prefix, Key>>, Config[Key], AvailableOutputStates, Depth>;
2565
+ };
2566
+ type HandlerTreeNodeValidationErrors<Validations> = {
2567
+ readonly [Key in keyof Validations as unknown extends Validations[Key] ? never : Key]?: Validations[Key];
2568
+ };
2569
+ type HandlerTreeNodeValidations<AllStates extends StateSchemas, States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, Prefix extends string, Config, AvailableOutputStates extends StateIdentifier<AllStates>, Depth extends ReadonlyArray<unknown>> = HandlerTreeNodeValidationMap<AllStates, States, Events, Emits, Prefix, Config, AvailableOutputStates, Depth> extends infer Validations ? HandlerTreeNodeValidationErrors<Validations> extends infer Errors ? keyof Errors extends never ? unknown : Errors : never : never;
2570
+ type HandlerTreeValidation<AllStates extends StateSchemas, States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, Prefix extends string, Config, AvailableOutputStates extends StateIdentifier<AllStates>, Depth extends ReadonlyArray<unknown> = HandlerDepth> = HandlerUnknownStateKeyValidation<States, Prefix, Config> & HandlerTreeNodeValidations<AllStates, States, Events, Emits, Prefix, Config, AvailableOutputStates, Depth>;
2155
2571
  type HandlerNodeChildrenConfig<Config> = "states" extends keyof Config ? Config extends {
2156
2572
  readonly states?: infer Children;
2157
2573
  } ? NonNullable<Children> : never : never;
2158
2574
  type HandlerTreeStateIds<AllStates extends StateSchemas, States extends StateSchemas, Prefix extends string, Config, Depth extends ReadonlyArray<unknown> = HandlerDepth> = {
2159
- readonly [Key in Extract<Extract<keyof Config, string>, Extract<keyof States, string>>]: HandlerStateId<AllStates, JoinPath<Prefix, Key>> | HandlerNodeChildStateIds<AllStates, States[Key], HandlerStateId<AllStates, JoinPath<Prefix, Key>>, Config[Key], Depth>;
2575
+ readonly [Key in Extract<Extract<keyof Config, string>, Extract<keyof States, string>>]: HandlerImplementedStateId<AllStates, States[Key], HandlerStateId<AllStates, JoinPath<Prefix, Key>>, Config[Key]> | HandlerNodeChildStateIds<AllStates, States[Key], HandlerStateId<AllStates, JoinPath<Prefix, Key>>, Config[Key], Depth>;
2160
2576
  }[Extract<Extract<keyof Config, string>, Extract<keyof States, string>>];
2577
+ type HandlerHasRequiredInitial<AllStates extends StateSchemas, StateId extends StateIdentifier<AllStates>, Config> = StateId extends RequiredHistoryInitializers<AllStates> ? "initial" extends keyof Config ? true : false : true;
2578
+ type HandlerHasRequiredHistoryDefaults<Node, Config> = Node extends {
2579
+ readonly states: infer Children extends StateSchemas;
2580
+ } ? [HistoryStateKey<Children>] extends [never] ? true : Config extends {
2581
+ readonly history?: infer HistoryConfig;
2582
+ } ? [
2583
+ Exclude<HistoryStateKey<Children>, Extract<keyof NonNullable<HistoryConfig>, string>>
2584
+ ] extends [never] ? true : false : false : true;
2585
+ type HandlerImplementedStateId<AllStates extends StateSchemas, Node, StateId extends StateIdentifier<AllStates>, Config> = [HistoryIdentifier<AllStates>] extends [never] ? StateId : HandlerHasRequiredInitial<AllStates, StateId, Config> extends true ? HandlerHasRequiredHistoryDefaults<Node, Config> extends true ? StateId : never : never;
2161
2586
  type HandlerNodeChildStateIds<AllStates extends StateSchemas, Node, Prefix extends string, Config, Depth extends ReadonlyArray<unknown>> = Depth extends readonly [] ? never : HandlerChildren<Node> extends infer Children extends StateSchemas ? [Children] extends [never] ? never : HandlerTreeStateIds<AllStates, Children, Prefix, HandlerNodeChildrenConfig<Config>, HandlerNextDepth<Depth>> : never;
2162
- type HandlerConfigError<Config> = Effect.Error<EventHandlerReturn<Config>> | Effect.Error<AlwaysReturn<Config>> | Effect.Error<DoneReturn<Config>> | Effect.Error<StateActionReturn<Config, "entry">> | Effect.Error<StateActionReturn<Config, "exit">> | InvokeError<Config>;
2587
+ type HandlerConfigError<Config> = Effect.Error<EventHandlerReturn<Config>> | Effect.Error<AlwaysReturn<Config>> | Effect.Error<DoneReturn<Config>> | Effect.Error<StateActionReturn<Config, "entry">> | Effect.Error<StateActionReturn<Config, "exit">> | Effect.Error<StateInitialReturn<Config>> | Effect.Error<HistoryDefaultReturn<Config>> | InvokeError<Config>;
2163
2588
  type HandlerTreeError<AllStates extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, States extends StateSchemas, Prefix extends string, Config, Depth extends ReadonlyArray<unknown> = HandlerDepth> = {
2164
2589
  readonly [Key in Extract<Extract<keyof Config, string>, Extract<keyof States, string>>]: HandlerConfigError<HandlerConfigPart<Config[Key]>> | HandlerNodeChildError<AllStates, Events, Emits, States[Key], HandlerStateId<AllStates, JoinPath<Prefix, Key>>, Config[Key], Depth>;
2165
2590
  }[Extract<Extract<keyof Config, string>, Extract<keyof States, string>>];
@@ -2186,7 +2611,7 @@ export declare namespace Machine {
2186
2611
  * @since 4.0.0
2187
2612
  */
2188
2613
  interface Handler<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, Input extends Schema.Top, UnhandledStates extends StateIdentifier<States>, E, R, InitialE, InitialR, FinalStates extends StateIdentifier<States>, Output, OutputStates extends StateIdentifier<States>, InputEvents extends ReadonlyArray<TaggedSchema>> {
2189
- <const Config extends HandlerTree<States, States, Events, Emits, E, R, "">>(config: Config & HandlerTreeValidation<States, States, Events, "", Config, OutputStates | Extract<HandlerTreeOutputStates<States, States, "", Config>, StateIdentifier<States>>> & EnsureCompatibleRuntime<HandlerTreeServices<States, Events, Emits, States, "", Config>, EventOf<Events>, EmitOf<Emits>>): HandleTreeResult<States, Events, Emits, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, OutputStates, InputEvents, Config>;
2614
+ <const Config extends HandlerTree<States, States, Events, Emits, E, R, "">>(config: Config & HandlerTreeValidation<States, States, Events, Emits, "", NoInfer<Config>, OutputStates | Extract<HandlerTreeOutputStates<States, States, "", NoInfer<Config>>, StateIdentifier<States>>>): HandleTreeResult<States, Events, Emits, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, OutputStates, InputEvents, Config>;
2190
2615
  }
2191
2616
  /**
2192
2617
  * Any state config.
@@ -2269,14 +2694,30 @@ export declare const isFinal: <const States extends Machine.StateSchemas, const
2269
2694
  * Machine.make({
2270
2695
  * states: States.states,
2271
2696
  * events: [],
2272
- * initial: () => States.initial.idle(new Idle({}))
2697
+ * initial: () => States.initial.idle.from()
2273
2698
  * })
2274
2699
  * ```
2275
2700
  *
2276
2701
  * @category constructors
2277
2702
  * @since 4.0.0
2278
2703
  */
2279
- export declare const defineStates: <const States extends Machine.StateSchemas>(states: States & DefineStateTreeInput<NoInfer<States>>, ..._validation: ValidateDefinedStates<NoInfer<States>>) => Machine.DefinedStates<States>;
2704
+ export declare const defineStates: DefineStates;
2705
+ type MakeConfig<States extends Machine.StateSchemas, InputEvents extends ReadonlyArray<Machine.TaggedSchema>, Emits extends ReadonlyArray<Machine.TaggedSchema>, Input extends Schema.Top, InitialE, InitialR, InternalEvents extends ReadonlyArray<Machine.TaggedSchema>> = {
2706
+ readonly id?: string;
2707
+ readonly states: States & DefineStateTreeInput<NoInfer<States>>;
2708
+ readonly events: InputEvents & ValidateInputEventProtocol<NoInfer<InputEvents>>;
2709
+ readonly internalEvents?: InternalEvents & ValidateInternalEventProtocol<NoInfer<InputEvents>, NoInfer<InternalEvents>>;
2710
+ readonly emits?: Emits;
2711
+ readonly input?: Input;
2712
+ readonly initial: (...args: [...Machine.InputArgs<Input>]) => Machine.InitialResult<States, InitialE, InitialR>;
2713
+ };
2714
+ type MakeResult<States extends Machine.StateSchemas, InputEvents extends ReadonlyArray<Machine.TaggedSchema>, Emits extends ReadonlyArray<Machine.TaggedSchema>, Input extends Schema.Top, InitialE, InitialR, InternalEvents extends ReadonlyArray<Machine.TaggedSchema>> = Machine<States, readonly [...InputEvents, ...InternalEvents], Input, Machine.StateIdentifier<States>, never, never, InitialE, InitialR, Machine.FinalStateFromDefinition<States>, Machine.TerminalOutput<States>, Emits, never, InputEvents>;
2715
+ interface Make {
2716
+ <const States extends Machine.StateSchemas, const InputEvents extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, InitialE = never, InitialR = never, const InternalEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []>(config: MakeConfig<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents>, ..._validation: ValidateDefinedStates<NoInfer<States>>): MakeResult<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents>;
2717
+ <const States extends Machine.StateSchemas, const InputEvents extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, InitialE = never, InitialR = never, const InternalEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []>(config: Omit<MakeConfig<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents>, "states"> & {
2718
+ readonly states: InvalidDefinedStateTreeInput<States>;
2719
+ }): never;
2720
+ }
2280
2721
  /**
2281
2722
  * Creates a schema-first machine definition.
2282
2723
  *
@@ -2327,17 +2768,7 @@ export declare const defineStates: <const States extends Machine.StateSchemas>(s
2327
2768
  * @category constructors
2328
2769
  * @since 4.0.0
2329
2770
  */
2330
- export declare const make: <const States extends Machine.StateSchemas, const InputEvents extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, InitialE = never, InitialR = never, const InternalEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []>(config: {
2331
- readonly id?: string;
2332
- readonly states: States & DefineStateTreeInput<NoInfer<States>>;
2333
- /** Events accepted through public machine input boundaries. */
2334
- readonly events: InputEvents;
2335
- /** Events delivered only by machine-local logic. */
2336
- readonly internalEvents?: InternalEvents;
2337
- readonly emits?: Emits;
2338
- readonly input?: Input;
2339
- readonly initial: (...args: [...Machine.InputArgs<Input>]) => Machine.InitialResult<States, InitialE, InitialR>;
2340
- }, ..._validation: [...ValidateDefinedStates<NoInfer<States>>, ...ValidateEventProtocol<NoInfer<InputEvents>, NoInfer<InternalEvents>>]) => Machine<States, readonly [...InputEvents, ...InternalEvents], Input, Machine.StateIdentifier<States>, never, never, InitialE, InitialR, Machine.FinalStateFromDefinition<States>, Machine.TerminalOutput<States>, Emits, never, InputEvents>;
2771
+ export declare const make: Make;
2341
2772
  /**
2342
2773
  * Encodes a decoded machine snapshot into a normalized data representation.
2343
2774
  *
@@ -2553,12 +2984,12 @@ type InvokeMachineInput<Input extends Schema.Top> = Input extends typeof Schema.
2553
2984
  */
2554
2985
  export declare const invokeMachine: {
2555
2986
  <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never, SnapshotEvent = never, DoneEvent = never, Id extends string = string, OutputStates extends Machine.StateIdentifier<States> = never, InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events>(config: {
2556
- readonly child: ChildMachine<Id, Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits, OutputStates, InputEvents> & Machine.EnsureOutputImplementations<States, OutputStates>>;
2987
+ readonly child: ChildMachine<Id, Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits, OutputStates, InputEvents> & Machine.EnsureOutputImplementations<States, OutputStates> & Machine.EnsureHistoryImplementations<States, UnhandledStates>>;
2557
2988
  readonly snapshot?: (context: Machine.InvokeSnapshotContext<Machine.Snapshot<States>, E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError, Output>) => SnapshotEvent | undefined;
2558
2989
  readonly onDone: (context: Machine.InvokeDoneContext<Output>) => DoneEvent | undefined;
2559
2990
  } & InvokeMachineInput<Input>): Machine.InvokeConfig<any, any, any, any, SnapshotEvent, Machine.Snapshot<States>, Machine.EventOf<InputEvents>, E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError, ExcludeCompatibleRuntime<Exclude<ExecutionServices<InitialR | R>, internalRuntime.MachineRuntime>, Machine.EventOf<Events>, Machine.EmitOf<Emits>>, Output, InitialE | E | ActionError<InitialR | R> | InfiniteTransitionError | MachineSchemaDecodeError | StartupError | StoppedError, Machine.EmitOf<Emits>, DoneEvent>;
2560
2991
  <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never, SnapshotEvent = never, Id extends string = string, OutputStates extends Machine.StateIdentifier<States> = never, InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events>(config: {
2561
- readonly child: ChildMachine<Id, Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits, OutputStates, InputEvents> & Machine.EnsureOutputImplementations<States, OutputStates>>;
2992
+ readonly child: ChildMachine<Id, Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits, OutputStates, InputEvents> & Machine.EnsureOutputImplementations<States, OutputStates> & Machine.EnsureHistoryImplementations<States, UnhandledStates>>;
2562
2993
  readonly snapshot?: (context: Machine.InvokeSnapshotContext<Machine.Snapshot<States>, E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError, Output>) => SnapshotEvent | undefined;
2563
2994
  readonly onDone?: never;
2564
2995
  } & InvokeMachineInput<Input>): Machine.InvokeConfig<any, any, any, any, SnapshotEvent, Machine.Snapshot<States>, Machine.EventOf<InputEvents>, E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError, ExcludeCompatibleRuntime<Exclude<ExecutionServices<InitialR | R>, internalRuntime.MachineRuntime>, Machine.EventOf<Events>, Machine.EmitOf<Emits>>, Output, InitialE | E | ActionError<InitialR | R> | InfiniteTransitionError | MachineSchemaDecodeError | StartupError | StoppedError, Machine.EmitOf<Emits>>;
@@ -2584,7 +3015,7 @@ export declare const invokeMachine: {
2584
3015
  * @category constructors
2585
3016
  * @since 4.0.0
2586
3017
  */
2587
- export declare const planInitial: <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never, OutputStates extends Machine.StateIdentifier<States> = never, InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events>(machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits, OutputStates, InputEvents> & Machine.EnsureOutputImplementations<States, OutputStates>, ...args: [...Machine.InputArgs<Input>]) => Effect.Effect<{
3018
+ export declare const planInitial: <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never, OutputStates extends Machine.StateIdentifier<States> = never, InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events>(machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits, OutputStates, InputEvents> & Machine.EnsureOutputImplementations<States, OutputStates> & Machine.EnsureHistoryImplementations<States, UnhandledStates>, ...args: [...Machine.InputArgs<Input>]) => Effect.Effect<{
2588
3019
  readonly state: Machine.Snapshot<States>;
2589
3020
  readonly actions: ReadonlyArray<Effect.Effect<void, ActionError<InitialR | R>, ActionServices<InitialR | R>>>;
2590
3021
  readonly emittedEvents: ReadonlyArray<Machine.EmitOf<Emits>>;
@@ -2625,7 +3056,7 @@ export declare const enabled: <const States extends Machine.StateSchemas, const
2625
3056
  * @category combinators
2626
3057
  * @since 4.0.0
2627
3058
  */
2628
- export declare const plan: <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never, OutputStates extends Machine.StateIdentifier<States> = never, InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events>(machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits, OutputStates, InputEvents> & Machine.EnsureOutputImplementations<States, OutputStates>, state: Machine.Snapshot<States>, event: Machine.EventOf<InputEvents>) => Effect.Effect<{
3059
+ export declare const plan: <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never, OutputStates extends Machine.StateIdentifier<States> = never, InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events>(machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits, OutputStates, InputEvents> & Machine.EnsureOutputImplementations<States, OutputStates> & Machine.EnsureHistoryImplementations<States, UnhandledStates>, state: Machine.Snapshot<States>, event: Machine.EventOf<InputEvents>) => Effect.Effect<{
2629
3060
  readonly next: Machine.Snapshot<States>;
2630
3061
  readonly actions: ReadonlyArray<Effect.Effect<void, ActionError<R>, ActionServices<R>>>;
2631
3062
  readonly emittedEvents: ReadonlyArray<Machine.EmitOf<Emits>>;
@@ -2899,5 +3330,5 @@ export declare const watch: <State, Event, Error = never, Output = never>(ref: M
2899
3330
  * @category constructors
2900
3331
  * @since 4.0.0
2901
3332
  */
2902
- export declare const start: <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never, OutputStates extends Machine.StateIdentifier<States> = never, InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events>(machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits, OutputStates, InputEvents> & Machine.EnsureOutputImplementations<States, OutputStates>, ...args: [...Machine.InputArgs<Input>]) => Effect.Effect<MachineRef<Machine.Snapshot<States>, Machine.EventOf<InputEvents>, E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError, Output>, InitialE | E | ActionError<InitialR | R> | InfiniteTransitionError | MachineSchemaDecodeError | StartupError | StoppedError, ExcludeCompatibleRuntime<ExecutionServices<InitialR | R>, Machine.EventOf<Events>, Machine.EmitOf<Emits>>>;
3333
+ export declare const start: <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never, OutputStates extends Machine.StateIdentifier<States> = never, InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events>(machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits, OutputStates, InputEvents> & Machine.EnsureOutputImplementations<States, OutputStates> & Machine.EnsureHistoryImplementations<States, UnhandledStates>, ...args: [...Machine.InputArgs<Input>]) => Effect.Effect<MachineRef<Machine.Snapshot<States>, Machine.EventOf<InputEvents>, E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError, Output>, InitialE | E | ActionError<InitialR | R> | InfiniteTransitionError | MachineSchemaDecodeError | StartupError | StoppedError, ExcludeCompatibleRuntime<ExecutionServices<InitialR | R>, Machine.EventOf<Events>, Machine.EmitOf<Emits>>>;
2903
3334
  //# sourceMappingURL=Machine.d.ts.map