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)
// ... etcWhat 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.
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({}); | ||
| }); |
| }); | ||
|
|
||
| expect(changedBarMachine.initialState.context).toBeUndefined(); | ||
| expect(changedBarMachine.initialState.context).toEqual({ bar: 42 }); |
There was a problem hiding this comment.
❕ 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'); | ||
| }); | ||
| }); |
|
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
contextpassed intocreateMachine(...)orcreateModel(...)to be an object.Closes https://github.com/davidkpiano/xstate/projects/1#card-40598611