Skip to content

Bug: Property '[Symbol.observable]' is missing in type - useSelector TS error #3141

Description

@davydof

Description

Hi, I'm migrating from old @xstate/react: 0.8.* and updating the code

I set machine types by createMachine<ToggleMachineContext, ToggleMachineEvent>({}) and try to use useSelector to get value from machine context but I get an error :/

TS2345: Argument of type 'Interpreter<ToggleMachineContext, any, ToggleEvent, { value: any; context: ToggleMachineContext; }, TypegenDisabled>'
is not assignable to parameter of type 'ActorRef<any, any>'.

Property '[Symbol.observable]' is missing in type
'Interpreter<ToggleMachineContext, any, ToggleEvent, { value: any; context: ToggleMachineContext; }, TypegenDisabled>'
but required in type 'ActorRef<any, any>'. 

types.d.ts(981, 5): '[Symbol.observable]' is declared here.

What I do wrong?
Could you please provide the example with working TS types?

Thank you!

In codesandbox I get another error :/

Expected result

No errors expected

Actual result

image

Reproduction

https://codesandbox.io/s/still-breeze-8iqd6w?file=/src/index.tsx

Additional context

import { useMachine, useSelector } from '@xstate/react';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { assign, createMachine } from 'xstate';

interface ToggleMachineContext {
  count: number;
}

interface ToggleEvent {
  type: 'TOGGLE';
}

type ToggleMachineEvent = ToggleEvent;

const toggleMachine = createMachine<ToggleMachineContext, ToggleMachineEvent>({
  id: 'toggle',
  initial: 'inactive',
  context: {
    count: 0,
  },
  states: {
    inactive: {
      on: { TOGGLE: 'active' },
    },
    active: {
      entry: assign({ count: (ctx) => ctx.count + 1 }),
      on: { TOGGLE: 'inactive' },
    },
  },
});

function App() {
  const [state, send, service] = useMachine(toggleMachine);
  const active = state.matches('active');

  const countFromSelector = useSelector(
    service,
    ({ context }) => context.count,
  );
  console.log('countFromSelector', countFromSelector);

  const count = state.context.count;
  console.log('count', count);

  return (
    <div className="App">
      <h1>XState React Template</h1>
      <h2>Fork this template!</h2>
      <button onClick={() => send('TOGGLE')}>
        Click me ({active ? '✅' : '❌'})
      </button>{' '}
      <code>
        Toggled <strong>{count}</strong> times
      </code>
    </div>
  );
}

const rootElement = document.getElementById('root');
ReactDOM.render(<App />, rootElement);
@xstate/react: 1.6.3
typescript: 3.9.9
xstate: 4.30.5

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions