mattpocock / xstate-codegen

A codegen tool for 100% TS type-safety in XState
MIT License
245 stars 12 forks source link

Putting machine options in separate files? #91

Open PickleyD opened 3 years ago

PickleyD commented 3 years ago

I'd like to be able to define my machine options in separate files so I can swap out real/mock service implementations like the following:

interface HomePageProviderProps {
    machineOptions: any; // <-- How do I type this?
    children: React.ReactNode;
}

const HomePageProvider = ({ machineOptions, children }: HomePageProviderProps) => {

    const [state, send] = useMachine(homePageMachine, machineOptions)

    return <HomePageStateContext.Provider value={state}>
        <HomePageDispatchContext.Provider value={{}}>
            {children}
        </HomePageDispatchContext.Provider>
    </HomePageStateContext.Provider>
}

Is there an easy way to get the generated types for my machine options so I can replace this any?

PickleyD commented 3 years ago

Replacing any with HomePageMachineStateMachine<HomePageMachineContext, HomePageMachineEvent, "homePageMachine">["_options"] seems to do the job. I'm fine with this unless there is a more reader-friendly way of doing it which I'm overlooking.