-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: allow any action payload if action was never typed #5
Conversation
With this PR it is possible to pass any type and amount of arguments as long as the action function was never typed. |
Thanks again for the quick response and fix, @ivanhofer! If I'm understanding this correctly, if someone is using vanilla JS (no explicit type annotations) but still relying on the type declarations for IDE feedback, they will always be able to use any argument arity and types in event invocations. When using TypeScript, if the action method signatures are explicitly defined, you will only be able to use the specified arity / types. Correct? Assuming that's correct – I think this approach makes sense. Given the dynamic nature of JS and svelte-ism, and the way args can be forwarded to lifecycle events, it makes sense that the type validation would be less strict (allow for all possible argument passing scenarios) if the user has not explicitly defined the allowed args. |
After reviewing the code and tests, I believe there may be another small bug in the type defs. An action method can return a const machine = fsm('foo', {
foo: {
returnsString() { return 'bar'; },
returnsSymbol() { return Symbol.for('bar'); }
returnsUndefined() {}
}
}); …but the event invocations always return a machine.returnsString() // => 'bar' (action caused a transition to 'bar')
machine.returnsSymbol() // => Symbol.for('bar') (action caused a transition to Symbol.for('bar') )
machine.returnsUndefined() // => 'foo' (action returned undefined, so there was no transition) Looking at our current type tests – it appears our type defs expect a Sorry… I should have caught this in the previous PR. Hoping this is relatively straightforward to fix 🤞? |
@kenkunz |
I think the confusion here stems from literally "speaking two different languages" (JS vs TS). In JS, (() => {})() === undefined; // true
void 0 === undefined; // true It TS (of which I am admittedly a novice and heavily relying on your expertise), I gather that Per the TS docs:
Excuse my ignorance in these matters… TS is relatively new to me. I'll try to be clear in the future if I'm "speaking" in JS vs. TS. If I don't specify, you can assume I'm speaking JS. Based on the last quoted paragraph above, I think you're correct in identifying In contrast to action functions (as defined in the fsm states |
@ivanhofer your changes in 18fdc8f look good – I'm going to merge this and close the PR & issue. Thanks again! Hopefully we won't see any other issues. I don't expect to make any API changes for a while – I'd like to see how this is used in the wild for a bit / what type of enhancement requests come in. |
@kenkunz and I didn't know it behaves like tis in JavaScript 😅 |
closes #4