Typing problem in invoke part #5182
Replies: 1 comment 1 reply
-
I think there are two major issues with your approach here:
If we change your code to avoid those major issues, we can see how much cleaner things become: import { assign, fromPromise, setup } from 'xstate'
import { LoginService } from './LoginService'
export interface FetchContext {
retries: number
loginService: LoginService
error: unknown
user: { userId: string } | undefined
}
export const loginStateMachine = setup({
types: {
context: {} as FetchContext,
events: {} as { type: 'FETCH'; email: string },
},
actors: {
login: fromPromise(
async ({ input }: { input: { email: string; loginService: any } }) => {
console.log(input)
return undefined
},
),
},
}).createMachine({
context: {
retries: 0,
loginService: new LoginService(),
error: undefined,
user: undefined,
},
id: 'auth',
initial: 'idle',
states: {
idle: {
on: {
FETCH: {
target: 'loading',
},
},
},
loading: {
invoke: [
{
id: 'login',
src: 'login',
input: ({ context, event }) => ({
email: event.email,
loginService: context.loginService,
}),
onDone: {
target: 'success',
actions: assign({ user: ({ event }) => event.output }),
},
onError: {
target: 'failure',
actions: assign({ error: ({ event }) => event.error }),
},
},
],
},
success: {
type: 'final',
},
failure: {
always: {
target: 'idle',
},
},
},
}) We also gain type safety in a number of locations that didn't previously exist:
In conclusion, unless you have a very specific reason for not defining your config inline and not defining your login inside |
Beta Was this translation helpful? Give feedback.
-
I tried different approaches, nothing helped, except setup any
but there is still a mistake in the idea
Argument type {readonly initial: "idle", readonly context: {retries: 0, loginService: LoginService, error: undefined, user: undefined}, readonly id: "auth", readonly states: {idle: {on: {FETCH: {target: "loading"}}}, success: {type: string}, failure: {always: {target: "idle"}}, loading: {invoke: any}}} is not assignable to parameter type MachineConfig<FetchContext, AnyEventObject, ToProvidedActor<{}, {}>, ToParameterizedObject<{}>, ToParameterizedObject<{}>, never, string, {} | null | undefined, {} | null | undefined, EventObject, MetaObject>
so I decided to ask the community for help
Beta Was this translation helpful? Give feedback.
All reactions