Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

machine.withConfig() returns a StateNode type instead of a StateMachine type, breaking event inference downstream #2239

Closed
VanTanev opened this issue May 25, 2021 · 1 comment

Comments

@VanTanev
Copy link
Contributor

VanTanev commented May 25, 2021

Description
StateMachine.withConfig() returns a StateNode instead of a StateMachine. This breaks interpreter inference.

import { createMachine, interpret } from 'xstate'

interface Context {}
type Event = { type: 'EVENT' }
const machine = createMachine<Context, Event>({})

const interpreter1: InterpreterFrom<typeof machine> = interpret(machine)

// @ts-expect-error Event object is not inferred properly for the Interpreter
const interpreter2: InterpreterFrom<typeof machine> = interpret(machine.withConfig({}))

Reproduction
https://codesandbox.io/s/machinewithcontext-returns-a-statenode-type-instead-of-a-statemachine-type-k2mxk

Fix?

export interface StateMachine<
  TContext,
  TStateSchema extends StateSchema,
  TEvent extends EventObject,
  TTypestate extends Typestate<TContext> = { value: any; context: TContext }
> extends StateNode<TContext, TStateSchema, TEvent, TTypestate> {
  id: string;
  states: StateNode<TContext, TStateSchema, TEvent>['states'];

  withConfig(
    options: Partial<MachineOptions<TContext, TEvent>>,
    context?: TContext
  ): StateMachine<TContext, TStateSchema, TEvent>;

  withContext(
    context: TContext
  ): StateMachine<TContext, TStateSchema, TEvent>
}
@VanTanev
Copy link
Contributor Author

PR: #2240

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

No branches or pull requests

2 participants