-
-
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
Ensure that context is defined when model type provided to createMachine() #2334
Conversation
🦋 Changeset detectedLatest commit: 38e6a5e 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 |
id: 'machine', | ||
initial: 'inactive', | ||
// missing context: | ||
// context: toggleModel.initialContext, |
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.
A question that I feel is somewhat related to this.
There are cases for which the initial context is not known upfront - for example when the machine requires user data but it can only be passed from the parent since it first needs to be acquired from somewhere. With this change, you force a user to provide some dummy data here or you steer them towards casting which loses the type safety. Of course, currently, there is no type safety when you don't provide any context value, and yet you expect context to be of a certain type but that is an orthogonal problem. And it has existed before the model has even been introduced.
So the question is - what the user should do when using modal for cases like the one that I've described. What's the official recommendation?
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.
With this pattern, that's not an issue:
const createUserMachine = user => createMachine({
context: { user }
});
And with a model:
const createUserMachine = user => {
const model = createModel({ user });
return createMachine<typeof model>({
context: model.initialContext
});
}
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.
Another idea (from Discord) for reference:
// TENTATIVE API
const machine = createMachine({
// input is of type Partial<TContext> maybe
context: (input) => ({
some: 'default value',
...input
})
});
machine.transition(/* ... */); // throws error; expecting input
machine.withContext({ the: 'input' }).transition(/* ... */);
interpret(machine); // throws error
interpret(machine.withContext({ ... })); // does not throw error
When using a model type in
createMachine<typeof someModel>(...)
, TypeScript will no longer compile machines that are missing thecontext
property in the machine configuration: