javascript-state-machine icon indicating copy to clipboard operation
javascript-state-machine copied to clipboard

Any typings for this cool library?

Open selganor74 opened this issue 9 years ago • 10 comments

I wasn't able to find any typescript typings for this lib... May I help in writing a suitable d.ts?

Thanks !

selganor74 avatar Jun 16 '16 09:06 selganor74

Did you find anything on this?

paralin avatar Aug 31 '16 21:08 paralin

There are typings on DefinitelyTyped

Everlag avatar Sep 07 '16 22:09 Everlag

the typings on DefinitelyTyped are for version 2.x. but the api has changed since 3.x

crysislinux avatar Jun 20 '17 07:06 crysislinux

+1

pawellen avatar Jul 16 '17 11:07 pawellen

Would it be of interest if I converted the project into Typescript for some extra well types, as well as automatic typings support?

AllNamesRTaken avatar Aug 30 '17 16:08 AllNamesRTaken

Maybe, but I'm using JavaScript now。

At 2017-08-31 00:05:14, "AllNamesRTaken" [email protected] wrote:

Would it be of interest if I converted the project into Typescript for some extra well types, as well as automatic typings support?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

sky068 avatar Sep 08 '17 07:09 sky068

https://github.com/jakesgordon/javascript-state-machine/pull/134 I added definition for 3.x, but it seems the author has no interest in this. it have been long time since i committed the pr.

taoqf avatar Nov 03 '17 03:11 taoqf

#134 I added definition for 3.x, but it seems the author has no interest in this. it have been long time since i committed the pr.

really helps! 哥你太酷了👍

bruceeewong avatar Apr 23 '21 08:04 bruceeewong

Version 4 is being developed, you can check out https://github.com/jakesgordon/javascript-state-machine/tree/4.0.0-beta

kevguy avatar Apr 23 '21 11:04 kevguy

In case anyone is interested, and while we are waiting for V4, I am using this typedef for V3 in my project: https://gist.github.com/QuentinRoy/94b2a102e45546774a9fc5656028c77a.

This type definition makes high use of Generic types. These are not intended to be provided by users, but instead automatically inferred by typescript. It is designed to be transparent, and to require virtually no modifications from existing code.

Still, I am rather proud of it as it is quite thorough, and is able to infer the possible state or transition values (not only string), and properly type the corresponding methods.

Known Caveats :

Transition Arguments are unknown Transition arguments other than lifecycle, i.e. the arguments provided to the transition methods, are all unknown as they are not currently statically inferred. Consequently they must be explicitly cast in observers or dynamic transitions (to functions) :

fsm.observe({
  onBeforeStart(lc, n) {
    // lc is properly typed, it even knows that transition can only be "start".
    console.log(`${lc.transition}: ${lc.from} -> ${lc.to}`);

    // n is unknown type, so we need to explicitly cast it to number.
    console.log(`n + 1: ${1 + (n as number)}`);
  },
});

// Also properly typed, except for the arguments whose type is unknown.
fsm.start(2);

It should be possible to let users define it as part of the generic arguments but due to current typescript limitation, this would force users to provide all generic types, and would not be quite user friendly. It should also be possible to infer arguments to transition methods from observers and to functions provided in the constructor (or factory), but it isn't easy.

Non finite state values are not typed The definition is quite efficient when the number of possible state is finite and can be specifically infer, e.g. "gas"|"liquid"|"solid", automatically typing onEnterGas, onGas, etc. This should cover the vast majority of use cases. But it is more complicated if you are making use of dynamic state types with to functions in your transitions.

Indeed, if the state values cannot be enumerated and their type is inferred as string, it won't be able to know the name of corresponding observers. These will need to be defined as regular method, and typed manually (the StateMachine.Lifecycle type should be useful to type the lifecycle argument). In fact, there is even a known bug as it would in this case erroneously define an on, onLeave, and an onEnter observer methods. I have not been able to prevent this at this moment.

If you do need to use dynamic state names and may use template literal to define how they look like, I strongly recommend you do it, e.g. specify that your to function returns step${number}. Typescript still won't be able to recognize the corresponding observers, but it won't create erroneous observer methods, and non overlapping state names will be properly recognized.

QuentinRoy avatar Apr 06 '22 10:04 QuentinRoy