Any typings for this cool library?
I wasn't able to find any typescript typings for this lib... May I help in writing a suitable d.ts?
Thanks !
Did you find anything on this?
There are typings on DefinitelyTyped
the typings on DefinitelyTyped are for version 2.x. but the api has changed since 3.x
+1
Would it be of interest if I converted the project into Typescript for some extra well types, as well as automatic typings support?
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.
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.
#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! 哥你太酷了👍
Version 4 is being developed, you can check out https://github.com/jakesgordon/javascript-state-machine/tree/4.0.0-beta
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.