-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[v5] setup()
API
#4353
[v5] setup()
API
#4353
Conversation
davidkpiano
commented
Oct 15, 2023
•
edited
Loading
edited
🦋 Changeset detectedLatest commit: 3bd3767 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
packages/core/test/assign.test.ts
Outdated
} | ||
}); | ||
|
||
p.createMachine({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fails without setup()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
types: {
children: {} as {
[key: `todo-${number}`]: 'todo';
['my-notifier']: 'notifierSrc';
}
}
Or:
types: {
childrenIds: {} as {
todo: `todo-${number}`,
notifierSrc: 'my-notifier',
}
}
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 3bd3767:
|
Big fan of this change! Would it be weird to also support setting the event type in |
That's the current plan.
I'm not sure if that would solve this particular issue though. The event type could be "preconfigured" here but other than that it would be set in stone. IIRC, you wanted to extend it with more event types. |
Don't forget to add |
Hey @davidkpiano, could you expand the example in the PR description to show envisioned usage for the other keys of the I really like the idea of defining the implementations up front and deriving types from them as much as possible but I'm struggling with thinking through how this could work with built-in actions like |
Hey sure! So, you can always inline const machine = setup({
actions: {
setToZero: assign({
count: 0
})
}
}).createMachine({
entry: 'setToZero'
}); Not sure how context typings will work, or if/how parameters will work with this yet (cc. @Andarist) |
This one works just fine with what I will be pushing out soon: setup({
types: {} as {
context: {
count: number;
};
},
actions: {
resetTo: assign((_, params: number) => ({
count: params
}))
}
}) |
In my case the file providing the preconfigured 'createMachine' (using setup) would be generated, so extra event types can be specified before generating the code. |
…r order guards (#4474) * Rework setup inference and specifically guard inferences to aid higher order guards * move tests * add extra runtime tests for guards execution * fixed `not` types * shift one extra type-level test * Remove `TParams` from `choose` and `pure`
THANK YOU for this! This cleared up quite a bit type inference I was doing. |
I'm just stumbling upon this from within the changelog. can someone give more context what this replaces or what the benefits of writing it that way are? is it just the call signature of thanks in advance |
By setting up the types in advance, not only is it much less boilerplate, but with how TypeScript works, we're able to more strongly infer typing throughout the machine. So instead of e.g.: createMachine({
types: {
actors: {} as {
logic: typeof someLogic;
}
}
}).provide({
actors: { someLogic }
}) You can write: setup({
actors: { someLogic }
}).createMachine({/* ... */}); Both
|
Thanks @davidkpiano for the swift response and clarification, love it! |