Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Best practice for typing dynamic machine configs#5104

Unanswered
AlexZasorin asked this question inQ&A
Discussion options

I'm currently working on a project where I'm storing machine configs in a database. These machines can take any form so we don't know what states or events they may have. I'm currently working on trying to improve the typing for these configs and would like to ask what the best practice for this is.

I'm looking for a generic machine config type to use for typing the input and return data for my database, as well as my test fixtures. I attempted to useUnknownMachineConfig, but passing in something typed asUnknownMachineConfig tocreateMachine gives a TypeScript error.createMachine seems to not acceptparams in ParamterizedObject andid in ProvidedActor being potentially undefined. I've gotten around this with:

// Makes the selected keys requiredexporttypePickRequired<T,KextendskeyofT>=Omit<T,K>&Required<Pick<T,K>>;// Stricter version of these two xstate types because `createMachine()` expects `params` to be required inside of//  ParameterizedObject and `id` to be required inside of `ProvidedActor`exporttypeStrictParameterizedObject=PickRequired<ParameterizedObject,'params'>;exporttypeStrictProvidedActor=PickRequired<ProvidedActor,'id'>;exporttypeGenericMachineConfig=MachineConfig<MachineContext,EventObject,StrictProvidedActor,StrictParameterizedObject,StrictParameterizedObject,string,string,any,unknown,EventObject,MetaObject>;

I'm not sure why I had to do this and it gives me the feeling that I'm approaching this wrong. Any advice?

You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

I found these two exported type while looking for a genericStateMachine type:

// xstate type definitionsexporttypeUnknownMachineConfig=MachineConfig<MachineContext,EventObject>;exporttypeAnyStateMachine=StateMachine<any,// contextany,// eventany,// childrenany,// actorany,// actionany,// guardany,// delayany,// state valueany,// tagany,// inputany,// outputany,// emittedany,// TMetaany>;

With the following import:

import{AnyStateMachine,UnknownMachineConfig}from'xstate';

However, I'm not sure this is relevant since there's no typing inference...

You must be logged in to vote
1 reply
@zahitrios
Comment options

Thanks@RemyMachado

I was struggling building a service capable to operate xstate state machines, something like strategy pattern using machines like strategies.

With typescript it was very difficult to define a generic type for every xstate state machine.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
3 participants
@AlexZasorin@RemyMachado@zahitrios

[8]ページ先頭

©2009-2025 Movatter.jp