Skip to content

Commit

Permalink
[core] Tighten StateMachine types and fix issue #5008 (#5009)
Browse files Browse the repository at this point in the history
* Add failing test

* Merge

* Make all StateMachine type params required

* Changeset?

* Update packages/core/src/types.ts

* Don't need the any check after all
  • Loading branch information
davidkpiano authored Jul 31, 2024
1 parent eac555e commit 51d4c4f
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 71 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-suits-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate': patch
---

The internal types for `StateMachine<...>` have been improved so that all type params are required, to prevent errors when using the types. This fixes weird issues like #5008.
23 changes: 14 additions & 9 deletions packages/core/src/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import type {
MetaObject,
StateSchema,
StateId,
SnapshotStatus
SnapshotStatus,
SnapshotFrom
} from './types.ts';
import { matchesState } from './utils.ts';

Expand Down Expand Up @@ -56,7 +57,7 @@ interface MachineSnapshotBase<
TTag extends string,
TOutput,
TMeta,
TConfig extends StateSchema = StateSchema
TConfig extends StateSchema
> {
/** The state machine that produced this state snapshot. */
machine: StateMachine<
Expand All @@ -72,7 +73,8 @@ interface MachineSnapshotBase<
unknown,
TOutput,
EventObject, // TEmitted
any // TMeta
any, // TMeta
TConfig
>;
/** The tags of the active state nodes that represent the current state value. */
tags: Set<string>;
Expand Down Expand Up @@ -198,7 +200,7 @@ interface ErrorMachineSnapshot<
TTag extends string,
TOutput,
TMeta extends MetaObject,
TConfig extends StateSchema = StateSchema
TConfig extends StateSchema
> extends MachineSnapshotBase<
TContext,
TEvent,
Expand All @@ -222,7 +224,7 @@ interface StoppedMachineSnapshot<
TTag extends string,
TOutput,
TMeta extends MetaObject,
TConfig extends StateSchema = StateSchema
TConfig extends StateSchema
> extends MachineSnapshotBase<
TContext,
TEvent,
Expand All @@ -246,7 +248,7 @@ export type MachineSnapshot<
TTag extends string,
TOutput,
TMeta extends MetaObject,
TConfig extends StateSchema = StateSchema
TConfig extends StateSchema
> =
| ActiveMachineSnapshot<
TContext,
Expand Down Expand Up @@ -355,7 +357,8 @@ export function createMachineSnapshot<
TChildren extends Record<string, AnyActorRef | undefined>,
TStateValue extends StateValue,
TTag extends string,
TMeta extends MetaObject
TMeta extends MetaObject,
TStateSchema extends StateSchema
>(
config: StateConfig<TContext, TEvent>,
machine: AnyStateMachine
Expand All @@ -366,7 +369,8 @@ export function createMachineSnapshot<
TStateValue,
TTag,
undefined,
TMeta
TMeta,
TStateSchema
> {
return {
status: config.status as never,
Expand Down Expand Up @@ -413,7 +417,8 @@ export function getPersistedSnapshot<
TStateValue,
TTag,
TOutput,
TMeta
TMeta,
any // state schema
>,
options?: unknown
): Snapshot<unknown> {
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/StateMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ export class StateMachine<
TTag extends string,
TInput,
TOutput,
TEmitted extends EventObject = EventObject, // TODO: remove default
TMeta extends MetaObject = MetaObject,
TConfig extends StateSchema = StateSchema
TEmitted extends EventObject,
TMeta extends MetaObject,
TConfig extends StateSchema
> implements
ActorLogic<
MachineSnapshot<
Expand Down Expand Up @@ -199,7 +199,8 @@ export class StateMachine<
TInput,
TOutput,
TEmitted,
TMeta
TMeta,
TConfig
> {
const { actions, guards, actors, delays } = this.implementations;

Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/StateNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ export class StateNode<
any, // input
any, // output
any, // emitted
any // meta
any, // meta
any // state schema
>;
/**
* The meta data associated with this state node, which will be returned in
Expand Down Expand Up @@ -368,7 +369,8 @@ export class StateNode<
any,
any,
any,
any // TMeta
any, // TMeta
any // TStateSchema
>,
event: TEvent
): TransitionDefinition<TContext, TEvent>[] | undefined {
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/createMachine.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { StateMachine } from './StateMachine.ts';
import { ResolvedStateMachineTypes } from './types.ts';
import { ResolvedStateMachineTypes, TODO } from './types.ts';
import {
AnyActorRef,
EventObject,
Expand Down Expand Up @@ -174,7 +174,8 @@ export function createMachine<
TInput,
TOutput,
TEmitted,
TMeta // TMeta
TMeta, // TMeta
TODO // TStateSchema
> {
return new StateMachine<
any,
Expand All @@ -189,6 +190,7 @@ export function createMachine<
any,
any,
any, // TEmitted
any // TMeta
any, // TMeta
any // TStateSchema
>(config as any, implementations as any);
}
20 changes: 16 additions & 4 deletions packages/core/src/stateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,8 @@ function transitionAtomicNode<
any,
any,
any,
any // TMeta
any, // TMeta
any // TStateSchema
>,
event: TEvent
): Array<TransitionDefinition<TContext, TEvent>> | undefined {
Expand All @@ -687,7 +688,8 @@ function transitionCompoundNode<
any,
any,
any,
any // TMeta
any, // TMeta
any // TStateSchema
>,
event: TEvent
): Array<TransitionDefinition<TContext, TEvent>> | undefined {
Expand Down Expand Up @@ -721,7 +723,8 @@ function transitionParallelNode<
any,
any,
any,
any // TMeta
any, // TMeta
any // TStateSchema
>,
event: TEvent
): Array<TransitionDefinition<TContext, TEvent>> | undefined {
Expand Down Expand Up @@ -758,7 +761,16 @@ export function transitionNode<
>(
stateNode: AnyStateNode,
stateValue: StateValue,
snapshot: MachineSnapshot<TContext, TEvent, any, any, any, any, any>,
snapshot: MachineSnapshot<
TContext,
TEvent,
any,
any,
any,
any,
any,
any // TStateSchema
>,
event: TEvent
): Array<TransitionDefinition<TContext, TEvent>> | undefined {
// leaf node
Expand Down
51 changes: 34 additions & 17 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ export interface UnifiedArg<
StateValue,
string,
unknown,
TODO // TMeta
TODO, // TMeta
TODO // State schema
>,
TEvent,
AnyEventObject
Expand Down Expand Up @@ -139,7 +140,8 @@ export type InputFrom<T> = T extends StateMachine<
infer TInput,
infer _TOutput,
infer _TEmitted,
infer _TMeta
infer _TMeta,
infer _TStateSchema
>
? TInput
: T extends ActorLogic<
Expand Down Expand Up @@ -1122,7 +1124,8 @@ export type AnyStateMachine = StateMachine<
any, // input
any, // output
any, // emitted
any // TMeta
any, // TMeta
any // TStateSchema
>;

export type AnyStateConfig = StateConfig<any, AnyEventObject>;
Expand Down Expand Up @@ -1321,7 +1324,8 @@ export type ContextFactory<
StateValue,
string,
unknown,
TODO // TMeta
TODO, // TMeta
TODO // State schema
>,
TEvent,
AnyEventObject
Expand Down Expand Up @@ -1627,7 +1631,8 @@ export type Mapper<
StateValue,
string,
unknown,
TODO // TMeta
TODO, // TMeta
TODO // State schema
>,
TEvent,
AnyEventObject
Expand Down Expand Up @@ -1727,8 +1732,9 @@ export interface StateConfig<
any,
any,
any,
any,
any, // TMeta
any
any // TStateSchema
>;
}

Expand Down Expand Up @@ -1990,8 +1996,9 @@ export type ActorLogicFrom<T> = ReturnTypeOrValue<T> extends infer R
any,
any,
any,
any,
any, // TMeta
any
any // TStateSchema
>
? R
: R extends Promise<infer U>
Expand All @@ -2013,7 +2020,8 @@ export type ActorRefFrom<T> = ReturnTypeOrValue<T> extends infer R
infer _TInput,
infer TOutput,
infer TEmitted,
infer TMeta
infer TMeta,
infer TStateSchema
>
? ActorRef<
MachineSnapshot<
Expand All @@ -2023,7 +2031,8 @@ export type ActorRefFrom<T> = ReturnTypeOrValue<T> extends infer R
TStateValue,
TTag,
TOutput,
TMeta
TMeta,
TStateSchema
>,
TEvent,
TEmitted
Expand Down Expand Up @@ -2059,7 +2068,8 @@ export type InterpreterFrom<
infer TInput,
infer TOutput,
infer TEmitted,
infer TMeta
infer TMeta,
infer TStateSchema
>
? Actor<
ActorLogic<
Expand All @@ -2070,7 +2080,8 @@ export type InterpreterFrom<
TStateValue,
TTag,
TOutput,
TMeta
TMeta,
TStateSchema
>,
TEvent,
TInput,
Expand All @@ -2095,7 +2106,8 @@ export type MachineImplementationsFrom<
infer _TInput,
infer _TOutput,
infer TEmitted,
infer _TMeta
infer _TMeta,
infer _TStateSchema
>
? InternalMachineImplementations<
ResolvedStateMachineTypes<
Expand Down Expand Up @@ -2311,7 +2323,8 @@ type ResolveEventType<T> = ReturnTypeOrValue<T> extends infer R
infer _TInput,
infer _TOutput,
infer _TEmitted,
infer _TMeta
infer _TMeta,
infer _TStateSchema
>
? TEvent
: R extends MachineSnapshot<
Expand All @@ -2321,7 +2334,8 @@ type ResolveEventType<T> = ReturnTypeOrValue<T> extends infer R
infer _TStateValue,
infer _TTag,
infer _TOutput,
infer _TMeta
infer _TMeta,
infer _TStateSchema
>
? TEvent
: R extends ActorRef<infer _TSnapshot, infer TEvent, infer _TEmitted>
Expand Down Expand Up @@ -2349,7 +2363,8 @@ export type ContextFrom<T> = ReturnTypeOrValue<T> extends infer R
infer _TInput,
infer _TOutput,
infer _TEmitted,
infer _TMeta
infer _TMeta,
infer _TStateSchema
>
? TContext
: R extends MachineSnapshot<
Expand All @@ -2359,7 +2374,8 @@ export type ContextFrom<T> = ReturnTypeOrValue<T> extends infer R
infer _TStateValue,
infer _TTag,
infer _TOutput,
infer _TMeta
infer _TMeta,
infer _TStateSchema
>
? TContext
: R extends Actor<infer TActorLogic>
Expand All @@ -2371,12 +2387,13 @@ export type ContextFrom<T> = ReturnTypeOrValue<T> extends infer R
infer _TAction,
infer _TGuard,
infer _TDelay,
infer _TStateValue,
infer _TTag,
infer _TInput,
infer _TOutput,
infer _TEmitted,
infer _TMeta,
infer _TTypes
infer _TStateSchema
>
? TContext
: never
Expand Down
Loading

0 comments on commit 51d4c4f

Please sign in to comment.