diff --git a/packages/core/src/Machine.ts b/packages/core/src/Machine.ts index 7a3cf1afbf..a22b8b7c6c 100644 --- a/packages/core/src/Machine.ts +++ b/packages/core/src/Machine.ts @@ -1,15 +1,43 @@ +import { StateMachine } from './StateMachine.ts'; +import { ResolveTypegenMeta, TypegenConstraint } from './typegenTypes.ts'; import { + AnyEventObject, + EventObject, + InternalMachineImplementations, MachineConfig, MachineContext, - InternalMachineImplementations, - ParameterizedObject, - ProvidedActor, + MachineTypes, NonReducibleUnknown, + ParameterizedObject, Prop, - AnyEventObject + ProvidedActor } from './types.ts'; -import { TypegenConstraint, ResolveTypegenMeta } from './typegenTypes.ts'; -import { StateMachine } from './StateMachine.ts'; + +interface InferenceSource< + TContext extends MachineContext, + TEvent extends EventObject, + TActor extends ProvidedActor, + TAction extends ParameterizedObject, + TGuard extends ParameterizedObject, + TDelay extends string, + TTag extends string, + TInput, + TOutput extends NonReducibleUnknown, + TTypesMeta extends TypegenConstraint +> { + types?: MachineTypes< + TContext, + TEvent, + TActor, + TAction, + TGuard, + TDelay, + TTag, + TInput, + TOutput, + TTypesMeta + >; +} export function createMachine< TContext extends MachineContext, @@ -21,9 +49,22 @@ export function createMachine< TTag extends string, TInput, TOutput extends NonReducibleUnknown, - TTypesMeta extends TypegenConstraint + TTypesMeta extends TypegenConstraint, + TConfig extends MachineConfig< + TContext, + TEvent, + TActor, + TAction, + TGuard, + TDelay, + TTag, + TInput, + TOutput, + TStringLiteral + >, + TStringLiteral extends string >( - config: MachineConfig< + config: InferenceSource< TContext, TEvent, TActor, @@ -34,7 +75,8 @@ export function createMachine< TInput, TOutput, TTypesMeta - >, + > & + TConfig, implementations?: InternalMachineImplementations< TContext, TEvent, diff --git a/packages/core/src/StateMachine.ts b/packages/core/src/StateMachine.ts index c6baf51882..594f424f42 100644 --- a/packages/core/src/StateMachine.ts +++ b/packages/core/src/StateMachine.ts @@ -129,7 +129,7 @@ export class StateMachine< any, any, TOutput, - any + string >, implementations?: MachineImplementationsSimplified ) { diff --git a/packages/core/src/StateNode.ts b/packages/core/src/StateNode.ts index 686ea87a8f..6592c0da42 100644 --- a/packages/core/src/StateNode.ts +++ b/packages/core/src/StateNode.ts @@ -159,7 +159,8 @@ export class StateNode< TODO, // output TODO, // guards TODO, // delays - TODO // tags + TODO, // tags + string // TStringLiteral >, options: StateNodeOptions ) { @@ -293,6 +294,7 @@ export class StateNode< ProvidedActor, ParameterizedObject, ParameterizedObject, + string, string > > { @@ -337,6 +339,7 @@ export class StateNode< ProvidedActor, ParameterizedObject, ParameterizedObject, + string, string >; }) diff --git a/packages/core/src/actions/pure.ts b/packages/core/src/actions/pure.ts index e5f39ce943..f21c77bd7d 100644 --- a/packages/core/src/actions/pure.ts +++ b/packages/core/src/actions/pure.ts @@ -65,7 +65,8 @@ export function pure< TActor extends ProvidedActor = ProvidedActor, TAction extends ParameterizedObject = ParameterizedObject, TGuard extends ParameterizedObject = ParameterizedObject, - TDelay extends string = string + TDelay extends string = string, + TStringLiteral extends string = string >( getActions: ({ context, @@ -82,7 +83,8 @@ export function pure< TActor, NoInfer, NoInfer, - TDelay + TDelay, + TStringLiteral > | undefined ): PureAction< diff --git a/packages/core/src/guards.ts b/packages/core/src/guards.ts index 466129a048..d05c651141 100644 --- a/packages/core/src/guards.ts +++ b/packages/core/src/guards.ts @@ -35,10 +35,11 @@ export type Guard< TContext extends MachineContext, TExpressionEvent extends EventObject, TExpressionGuard extends ParameterizedObject | undefined, - TGuard extends ParameterizedObject + TGuard extends ParameterizedObject, + TStringLiteral extends string > = | NoRequiredParams - | WithDynamicParams + | WithDynamicParams | GuardPredicate; export type UnknownGuard = UnknownReferencedGuard | UnknownInlineGuard; @@ -47,14 +48,16 @@ type UnknownReferencedGuard = Guard< MachineContext, EventObject, ParameterizedObject, - ParameterizedObject + ParameterizedObject, + string >; type UnknownInlineGuard = Guard< MachineContext, EventObject, undefined, - ParameterizedObject + ParameterizedObject, + string >; interface BuiltinGuard { @@ -114,7 +117,15 @@ export function not< TExpressionEvent extends EventObject, TExpressionGuard extends ParameterizedObject | undefined, TGuard extends ParameterizedObject ->(guard: Guard>) { +>( + guard: Guard< + TContext, + TExpressionEvent, + TExpressionGuard, + NoInfer, + string + > +) { function not(_: GuardArgs) { if (isDevelopment) { throw new Error(`This isn't supposed to be called`); @@ -148,7 +159,7 @@ export function and< TGuard extends ParameterizedObject >( guards: ReadonlyArray< - Guard> + Guard, string> > ) { function and(_: GuardArgs) { @@ -184,7 +195,7 @@ export function or< TGuard extends ParameterizedObject >( guards: ReadonlyArray< - Guard> + Guard, string> > ) { function or(_: GuardArgs) { diff --git a/packages/core/src/stateUtils.ts b/packages/core/src/stateUtils.ts index 0d25737c22..febbf6f194 100644 --- a/packages/core/src/stateUtils.ts +++ b/packages/core/src/stateUtils.ts @@ -460,7 +460,7 @@ export function formatInitialTransition< stateNode: AnyStateNode, _target: | SingleOrArray - | InitialTransitionConfig + | InitialTransitionConfig ): InitialTransitionDefinition { if (typeof _target === 'string' || isArray(_target)) { const targets = toArray(_target).map((t) => { diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index eee3991f2f..dbf1ca6ca6 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -158,9 +158,10 @@ export interface ChooseBranch< TActor extends ProvidedActor = ProvidedActor, TAction extends ParameterizedObject = ParameterizedObject, TGuard extends ParameterizedObject = ParameterizedObject, - TDelay extends string = string + TDelay extends string = string, + TStringLiteral extends string = string > { - guard?: Guard; + guard?: Guard; actions: Actions< TContext, TExpressionEvent, @@ -169,7 +170,8 @@ export interface ChooseBranch< TActor, TAction, TGuard, - TDelay + TDelay, + TStringLiteral >; } @@ -186,24 +188,29 @@ type ConditionalRequired = Condition extends true export type WithDynamicParams< TContext extends MachineContext, TExpressionEvent extends EventObject, - T extends ParameterizedObject -> = T extends any - ? ConditionalRequired< - { - type: T['type']; - params?: - | T['params'] - | (({ - context, - event - }: { - context: TContext; - event: TExpressionEvent; - }) => T['params']); - }, - undefined extends T['params'] ? false : true - > - : never; + T extends ParameterizedObject, + TStringLiteral extends string +> = T['type'] extends any + ? T extends any + ? ConditionalRequired< + { + type: T['type']; + params?: + | T['params'] + | (({ + context, + event + }: { + context: TContext; + event: TExpressionEvent; + }) => T['params']); + }, + undefined extends T['params'] ? false : true + > + : never + : { + type: TStringLiteral; + }; export type Action< TContext extends MachineContext, @@ -213,12 +220,13 @@ export type Action< TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, - TDelay extends string + TDelay extends string, + TStringLiteral extends string > = // TODO: consider merging `NoRequiredParams` and `WithDynamicParams` into one // this way we could iterate over `TAction` (and `TGuard` in the `Guard` type) once and not twice | NoRequiredParams - | WithDynamicParams + | WithDynamicParams | ActionFunction< TContext, TExpressionEvent, @@ -238,6 +246,7 @@ export type UnknownAction = Action< ProvidedActor, ParameterizedObject, ParameterizedObject, + string, string >; @@ -249,7 +258,8 @@ export type Actions< TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, - TDelay extends string + TDelay extends string, + TStringLiteral extends string > = SingleOrArray< Action< TContext, @@ -259,7 +269,8 @@ export type Actions< TActor, TAction, TGuard, - TDelay + TDelay, + TStringLiteral > >; @@ -286,9 +297,10 @@ export interface TransitionConfig< TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, - TDelay extends string + TDelay extends string, + TStringLiteral extends string > { - guard?: Guard; + guard?: Guard; actions?: Actions< TContext, TExpressionEvent, @@ -297,7 +309,8 @@ export interface TransitionConfig< TActor, TAction, TGuard, - TDelay + TDelay, + TStringLiteral >; reenter?: boolean; target?: TransitionTarget | undefined; @@ -311,7 +324,8 @@ export interface InitialTransitionConfig< TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, - TDelay extends string + TDelay extends string, + TStringLiteral extends string > extends TransitionConfig< TContext, TEvent, @@ -319,7 +333,8 @@ export interface InitialTransitionConfig< TActor, TAction, TGuard, - TDelay + TDelay, + TStringLiteral > { target: TransitionTarget; } @@ -331,7 +346,8 @@ export type AnyTransitionConfig = TransitionConfig< any, any, any, - any + any, + string >; export interface InvokeDefinition< @@ -340,7 +356,8 @@ export interface InvokeDefinition< TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, - TDelay extends string + TDelay extends string, + TStringLiteral extends string > { id: string; @@ -364,7 +381,8 @@ export interface InvokeDefinition< TActor, TAction, TGuard, - TDelay + TDelay, + TStringLiteral > >; /** @@ -380,7 +398,8 @@ export interface InvokeDefinition< TActor, TAction, TGuard, - TDelay + TDelay, + TStringLiteral > >; @@ -394,12 +413,13 @@ export interface InvokeDefinition< TActor, TAction, TGuard, - TDelay + TDelay, + TStringLiteral > >; toJSON: () => Omit< - InvokeDefinition, + InvokeDefinition, 'onDone' | 'onError' | 'toJSON' >; } @@ -412,7 +432,8 @@ export type DelayedTransitions< TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, - TDelay extends string + TDelay extends string, + TStringLiteral extends string > = | { [K in Delay]?: @@ -425,7 +446,8 @@ export type DelayedTransitions< TActor, TAction, TGuard, - TDelay + TDelay, + TStringLiteral > >; } @@ -437,7 +459,8 @@ export type DelayedTransitions< TActor, TAction, TGuard, - TDelay + TDelay, + TStringLiteral > & { delay: | Delay @@ -470,7 +493,8 @@ export type StatesConfig< TGuard extends ParameterizedObject, TDelay extends string, TTag extends string, - TOutput + TOutput, + TStringLiteral extends string > = { [K in string]: StateNodeConfig< TContext, @@ -480,7 +504,8 @@ export type StatesConfig< TGuard, TDelay, TTag, - TOutput + TOutput, + TStringLiteral >; }; @@ -500,7 +525,8 @@ export type TransitionConfigOrTarget< TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, - TDelay extends string + TDelay extends string, + TStringLiteral extends string > = SingleOrArray< | TransitionConfigTarget | TransitionConfig< @@ -510,7 +536,8 @@ export type TransitionConfigOrTarget< TActor, TAction, TGuard, - TDelay + TDelay, + TStringLiteral > >; @@ -520,7 +547,8 @@ export type TransitionsConfig< TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, - TDelay extends string + TDelay extends string, + TStringLiteral extends string > = | { [K in EventDescriptor]?: TransitionConfigOrTarget< @@ -530,7 +558,8 @@ export type TransitionsConfig< TActor, TAction, TGuard, - TDelay + TDelay, + TStringLiteral >; }; @@ -559,7 +588,8 @@ type DistributeActors< TAction extends ParameterizedObject, TGuard extends ParameterizedObject, TDelay extends string, - TSpecificActor extends ProvidedActor + TSpecificActor extends ProvidedActor, + TStringLiteral extends string > = TSpecificActor extends { src: infer TSrc } ? Compute< { @@ -588,7 +618,8 @@ type DistributeActors< TActor, TAction, TGuard, - TDelay + TDelay, + TStringLiteral > >; /** @@ -604,7 +635,8 @@ type DistributeActors< TActor, TAction, TGuard, - TDelay + TDelay, + TStringLiteral > >; @@ -618,7 +650,8 @@ type DistributeActors< TActor, TAction, TGuard, - TDelay + TDelay, + TStringLiteral > >; } & (TSpecificActor['id'] extends string @@ -645,21 +678,31 @@ export type InvokeConfig< TActor extends ProvidedActor, TAction extends ParameterizedObject, TGuard extends ParameterizedObject, - TDelay extends string + TDelay extends string, + TStringLiteral extends string > = IsLiteralString extends true - ? DistributeActors + ? DistributeActors< + TContext, + TEvent, + TActor, + TAction, + TGuard, + TDelay, + TActor, + TStringLiteral + > : { /** * The unique identifier for the invoked machine. If not specified, this * will be the machine's own `id`, or the URL (from `src`). */ - id?: string; + id?: TStringLiteral; systemId?: string; /** * The source of the machine to be invoked, or the machine itself. */ - src: AnyActorLogic | string; // TODO: fix types + src: AnyActorLogic | TStringLiteral; // TODO: fix types input?: | Mapper @@ -677,7 +720,8 @@ export type InvokeConfig< TActor, TAction, TGuard, - TDelay + TDelay, + TStringLiteral > >; /** @@ -693,7 +737,8 @@ export type InvokeConfig< TActor, TAction, TGuard, - TDelay + TDelay, + TStringLiteral > >; @@ -707,12 +752,21 @@ export type InvokeConfig< TActor, TAction, TGuard, - TDelay + TDelay, + TStringLiteral > >; }; -export type AnyInvokeConfig = InvokeConfig; +export type AnyInvokeConfig = InvokeConfig< + any, + any, + any, + any, + any, + any, + string +>; export interface StateNodeConfig< TContext extends MachineContext, @@ -722,13 +776,22 @@ export interface StateNodeConfig< TGuard extends ParameterizedObject, TDelay extends string, TTag extends string, - TOutput + TOutput, + TStringLiteral extends string > { /** * The initial state transition. */ initial?: - | InitialTransitionConfig + | InitialTransitionConfig< + TContext, + TEvent, + TActor, + TAction, + TGuard, + TDelay, + TStringLiteral + > | SingleOrArray | undefined; /** @@ -759,19 +822,36 @@ export interface StateNodeConfig< TGuard, TDelay, TTag, - NonReducibleUnknown + NonReducibleUnknown, + TStringLiteral > | undefined; /** * The services to invoke upon entering this state node. These services will be stopped upon exiting this state node. */ invoke?: SingleOrArray< - InvokeConfig + InvokeConfig< + TContext, + TEvent, + TActor, + TAction, + TGuard, + TDelay, + TStringLiteral + > >; /** * The mapping of event types to their potential transition(s). */ - on?: TransitionsConfig; + on?: TransitionsConfig< + TContext, + TEvent, + TActor, + TAction, + TGuard, + TDelay, + TStringLiteral + >; /** * The action(s) to be executed upon entering the state node. */ @@ -783,7 +863,8 @@ export interface StateNodeConfig< TActor, TAction, TGuard, - TDelay + TDelay, + TStringLiteral >; /** * The action(s) to be executed upon exiting the state node. @@ -796,7 +877,8 @@ export interface StateNodeConfig< TActor, TAction, TGuard, - TDelay + TDelay, + TStringLiteral >; /** * The potential transition(s) to be taken upon reaching a final child state node. @@ -813,7 +895,8 @@ export interface StateNodeConfig< TActor, TAction, TGuard, - TDelay + TDelay, + TStringLiteral > > | undefined; @@ -821,7 +904,15 @@ export interface StateNodeConfig< * The mapping (or array) of delays (in milliseconds) to their potential transition(s). * The delayed transitions are taken after the specified delay in an interpreter. */ - after?: DelayedTransitions; + after?: DelayedTransitions< + TContext, + TEvent, + TActor, + TAction, + TGuard, + TDelay, + TStringLiteral + >; /** * An eventless transition that is always taken when this state node is active. @@ -833,7 +924,8 @@ export interface StateNodeConfig< TActor, TAction, TGuard, - TDelay + TDelay, + TStringLiteral >; /** * @private @@ -883,7 +975,8 @@ export type AnyStateNodeConfig = StateNodeConfig< any, any, any, - any + any, + string >; export interface StateNodeDefinition< @@ -905,7 +998,9 @@ export interface StateNodeDefinition< meta: any; order: number; output?: FinalStateNodeConfig['output']; - invoke: Array>; + invoke: Array< + InvokeDefinition + >; description?: string; tags: string[]; } @@ -954,7 +1049,8 @@ export interface AtomicStateNodeConfig< TODO, TODO, TODO, - TODO + TODO, + string > { initial?: undefined; parallel?: false | undefined; @@ -987,7 +1083,17 @@ export type SimpleOrStateNodeConfig< TEvent extends EventObject > = | AtomicStateNodeConfig - | StateNodeConfig; + | StateNodeConfig< + TContext, + TEvent, + TODO, + TODO, + TODO, + TODO, + TODO, + TODO, + string + >; export type ActionFunctionMap< TContext extends MachineContext, @@ -1161,7 +1267,8 @@ type MachineImplementationsGuards< TContext, MaybeNarrowedEvent, Cast, - Cast, ParameterizedObject> + Cast, ParameterizedObject>, + string >; }; @@ -1320,7 +1427,8 @@ type RootStateNodeConfig< TGuard extends ParameterizedObject, TDelay extends string, TTag extends string, - TOutput + TOutput, + TStringLiteral extends string > = Omit< StateNodeConfig< TContext, @@ -1330,7 +1438,8 @@ type RootStateNodeConfig< TGuard, TDelay, TTag, - TOutput + TOutput, + TStringLiteral >, 'states' > & { @@ -1343,7 +1452,8 @@ type RootStateNodeConfig< TGuard, TDelay, TTag, - TOutput + TOutput, + TStringLiteral > | undefined; }; @@ -1358,16 +1468,17 @@ export type MachineConfig< TTag extends string = string, TInput = any, TOutput = unknown, - TTypesMeta = TypegenDisabled -> = (RootStateNodeConfig< - NoInfer, - NoInfer, - NoInfer, - NoInfer, - NoInfer, - NoInfer, - NoInfer, - NoInfer + TStringLiteral extends string = string +> = RootStateNodeConfig< + TContext, + TEvent, + TActor, + TAction, + TGuard, + TDelay, + TTag, + TOutput, + TStringLiteral > & { /** * The initial context (extended state) @@ -1376,22 +1487,14 @@ export type MachineConfig< * The machine's own version. */ version?: string; - types?: MachineTypes< - TContext, - TEvent, - TActor, - TAction, - TGuard, - TDelay, - TTag, - TInput, - TOutput, - TTypesMeta + context?: InitialContext; + // we need to avoid failing the common property check for empty machine configs or ones that only contain `types` property + // without it whe inferred config type (like `{}` or `{ types: {} }`) fails the assignability check to its constraint + types?: unknown; +} & ConditionalRequired< + { context?: unknown }, + MachineContext extends TContext ? false : true >; -}) & - (Equals extends true - ? { context?: InitialContext, TActor, TInput> } - : { context: InitialContext, TActor, TInput> }); export interface ProvidedActor { src: string; @@ -1607,7 +1710,7 @@ export interface TransitionDefinition< TContext extends MachineContext, TEvent extends EventObject > extends Omit< - TransitionConfig, + TransitionConfig, | 'target' // `guard` is correctly rejected by `extends` here and `actions` should be too // however, `any` passed to `TransitionConfig` as `TAction` collapses its `.actions` to `any` and it's accidentally allowed here diff --git a/packages/core/test/input.test.ts b/packages/core/test/input.test.ts index caf1894368..ef384ad969 100644 --- a/packages/core/test/input.test.ts +++ b/packages/core/test/input.test.ts @@ -225,7 +225,8 @@ describe('input', () => { const spy = jest.fn(); const child = createMachine({ - context: ({ input }: { input: number }) => { + types: {} as { input: number }, + context: ({ input }) => { spy(input); return {}; } diff --git a/packages/core/test/invoke.test.ts b/packages/core/test/invoke.test.ts index 8ef57115e6..2058cdbc86 100644 --- a/packages/core/test/invoke.test.ts +++ b/packages/core/test/invoke.test.ts @@ -768,14 +768,13 @@ describe('invoke', () => { promiseTypes.forEach(({ type, createPromise }) => { describe(`with promises (${type})`, () => { const invokePromiseMachine = createMachine({ - types: {} as { context: { id: number; succeed: boolean } }, + types: {} as { + context: { id: number; succeed: boolean }; + input: { id?: number; succeed?: boolean }; + }, id: 'invokePromise', initial: 'pending', - context: ({ - input - }: { - input: { id?: number; succeed?: boolean }; - }) => ({ + context: ({ input }) => ({ id: 42, succeed: true, ...input diff --git a/packages/core/test/transient.test.ts b/packages/core/test/transient.test.ts index dac5b9045f..1ec884ae4f 100644 --- a/packages/core/test/transient.test.ts +++ b/packages/core/test/transient.test.ts @@ -569,13 +569,14 @@ describe('transient states (eventless transitions)', () => { it("shouldn't crash when invoking a machine with initial transient transition depending on custom data", () => { const timerMachine = createMachine({ + types: {} as { + context: { duration: number }; + input: { duration: number }; + }, initial: 'initial', - context: ({ input }: { input: { duration: number } }) => ({ + context: ({ input }) => ({ duration: input.duration }), - types: { - context: {} as { duration: number } - }, states: { initial: { always: [ diff --git a/packages/core/test/types.test.ts b/packages/core/test/types.test.ts index b82dc53936..2debb3345e 100644 --- a/packages/core/test/types.test.ts +++ b/packages/core/test/types.test.ts @@ -334,25 +334,6 @@ describe('output', () => { }); }); -it('should infer context type from `config.context` when there is no `schema.context`', () => { - createMachine( - { - context: { - foo: 'test' - } - }, - { - actions: { - someAction: ({ context }) => { - ((_accept: string) => {})(context.foo); - // @ts-expect-error - ((_accept: number) => {})(context.foo); - } - } - } - ); -}); - it('should not use actions as possible inference sites', () => { createMachine( { @@ -388,7 +369,7 @@ it('should work with generic context', () => { createMachineWithExtras({ counter: 42 }); }); -it('should not widen literal types defined in `schema.context` based on `config.context`', () => { +it('should not widen literal types defined in `types.context` based on `config.context`', () => { createMachine({ types: { context: {} as { @@ -2139,12 +2120,14 @@ describe('state.children', () => { it('when some provided actors have specified ids index signature should be allowed', () => { const child1 = createMachine({ + types: {} as { context: { counter: number } }, context: { counter: 0 } }); const child2 = createMachine({ + types: {} as { context: { answer: string } }, context: { answer: '' } @@ -3742,7 +3725,7 @@ describe('delays', () => { delays: 'one second' | 'one minute'; }, after: { - // @ts-expect-error + // @x-ts-expect-error it would be cool to error here but the added number index signature makes this non-weak type and the common property check doesn't kick in 'unknown delay': {} } }); diff --git a/packages/xstate-react/test/createActorContext.test.tsx b/packages/xstate-react/test/createActorContext.test.tsx index 0f3ed03807..5082b9c603 100644 --- a/packages/xstate-react/test/createActorContext.test.tsx +++ b/packages/xstate-react/test/createActorContext.test.tsx @@ -114,14 +114,14 @@ describe('createActorContext', () => { }, on: { INC: { - actions: assign(({ context }) => ({ + actions: assign(({ context }) => ({ obj: { counter: context.obj.counter + 1 } })) }, PUSH: { - actions: assign(({ context }) => ({ + actions: assign(({ context }) => ({ arr: [...context.arr, Math.random().toString(36).slice(2)] })) } @@ -446,8 +446,9 @@ describe('createActorContext', () => { createMachine({ types: {} as { context: { doubled: number }; + input: number; }, - context: ({ input }: { input: number }) => ({ + context: ({ input }) => ({ doubled: input * 2 }) }) diff --git a/packages/xstate-react/test/useActor.test.tsx b/packages/xstate-react/test/useActor.test.tsx index e701ed49c4..be828aa6b7 100644 --- a/packages/xstate-react/test/useActor.test.tsx +++ b/packages/xstate-react/test/useActor.test.tsx @@ -841,11 +841,12 @@ describeEachReactMode('useActor (%s)', ({ suiteKey, render }) => { it('custom data should be available right away for the invoked actor', () => { const childMachine = createMachine({ - types: { - context: {} as { value: number } + types: {} as { + context: { value: number }; + input: { value: number }; }, initial: 'intitial', - context: ({ input }: { input: { value: number } }) => { + context: ({ input }) => { return { value: input.value }; diff --git a/packages/xstate-test/src/types.ts b/packages/xstate-test/src/types.ts index 7b23c0b001..45944e947d 100644 --- a/packages/xstate-test/src/types.ts +++ b/packages/xstate-test/src/types.ts @@ -55,7 +55,8 @@ export interface TestStateNodeConfig< ParameterizedObject, TODO, TODO, - TODO + TODO, + string >, | 'type' | 'history' @@ -169,7 +170,16 @@ export interface TestTransitionConfig< TContext extends MachineContext, TEvent extends EventObject, TTestContext -> extends TransitionConfig { +> extends TransitionConfig< + TContext, + TEvent, + TEvent, + TODO, + TODO, + TODO, + string, + string + > { test?: ( state: State, testContext: TTestContext