-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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] Restrict context
to object
#2294
Conversation
🦋 Changeset detectedLatest commit: 5cd04ee The changes in this PR will be included in the next version bump. This PR includes changesets to release 9 packages
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 |
In typescript, type X = object | undefined
let a: X = []
let b: X = new Map()
let c: X = new String('a')
let d: X = new Number(1)
let e: X = new Number(NaN)
let f: X = new Boolean(false)
// ... etc What is the restriction you're trying to apply? |
Only literal objects |
Unfortunately, as far as I know, there is no such type. I think that usually |
Yep, this is a very unfortunate (and quite frankly - misleading) quirk of TS. Some simple examples showcased here. I think the I wonder though if we have to support
I think that defaulting to an empty object could potentially lead to less special-casing certain code paths in the implementation and that there is no much value in supporting undefined contexts. |
context
to object or undefinedcontext
to object
${scope} | ||
${body} | ||
${scope} | ||
with (context) { |
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.
Asking for forgiveness for using the deprecated with
statement, but ✨ it works so well ✨
it('machines defined without context should have a default empty object for context', () => { | ||
const machine = createMachine({}); | ||
|
||
expect(machine.initialState.context).toEqual({}); | ||
}); |
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.
❕ New test
@@ -239,7 +245,7 @@ describe('machine', () => { | |||
bar: 42 | |||
}); | |||
|
|||
expect(changedBarMachine.initialState.context).toBeUndefined(); | |||
expect(changedBarMachine.initialState.context).toEqual({ bar: 42 }); |
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.
❕ Changed test outcome (context
is no longer undefined
ever)
describe('types', () => { | ||
it('defined context in createMachine() should be an object', () => { | ||
createMachine({ | ||
// @ts-expect-error | ||
context: 'string' | ||
}); | ||
}); | ||
|
||
it('defined context passed to createModel() should be an object', () => { | ||
// @ts-expect-error | ||
createModel('string'); | ||
}); | ||
}); |
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.
❕ New tests
Reviewers: you can safely ignore most of the changes in these files (there's a ton), because they're mostly changing the types to enforce that Look for the comments with an ❕ icon. |
Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
This PR restricts
context
passed intocreateMachine(...)
orcreateModel(...)
to be an object.Closes https://github.com/davidkpiano/xstate/projects/1#card-40598611