#Types for createMachine and service in a class

1 messages · Page 1 of 1 (latest)

hexed pier
#

Hello I've been using createMachine and services and placed it into a class like the following:

class Test {
    machine: any; // What to put here?
    service: any; // What to put here? 
    constructor() {
        this.machine =
            createMachine({
                // tsTypes: {}, 
                id: "(machine)",
            },
                {
                })
        this.service = interpret(this.machine).onTransition((state, event) => {

        }).start()

    }
}

const test = new Test()

However the type that is inferred here goes to any when using VS code to generate the declaration.

Using ReturnType kinda works for machine: machine: ReturnType<typeof createMachine> for machine, but for interpret it doesn't quite work the same.

What would I need to put in place of the any to get all the type info?

paper cove
#

If you can create the machine outside the class, you can use these types:

class Test {
  machine: typeof someMachine;
  service: InterpreterFrom<typeof someMachine>;

  // ...
}
hexed pier
#

Ah my bad, the machine has to be decoupled from the class so I can put in my own machines. Updated example:

class Test{
    machine: any; // What to put here? 
    service: any; // What to put here? 
    constructor(machine: any) {
        this.machine = machine
        this.service = interpret(this.machine)
        this.service.onTransition((state:any, event: any) => {
        }).start()

    }
}

const m = createMachine({
    id: "(machine)",
})
const test = new Test(m)

Thinking maybe I can use generic types, but I'm having trouble getting it working...