Skip to content

Commit

Permalink
[core] Fix EventFrom and ContextFrom (#5034)
Browse files Browse the repository at this point in the history
* Fix StateSchema

* Add ContextFrom test

* Update packages/core/test/setup.types.test.ts

Co-authored-by: Mateusz Burzyński <[email protected]>

* Update packages/core/test/setup.types.test.ts

Co-authored-by: Mateusz Burzyński <[email protected]>

* Changeset

---------

Co-authored-by: Mateusz Burzyński <[email protected]>
  • Loading branch information
davidkpiano and Andarist authored Aug 13, 2024
1 parent 12bde7a commit 7bed484
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-spoons-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate': patch
---

Fix `EventFrom` and `ContextFrom` types
16 changes: 16 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2467,6 +2467,22 @@ export type ToChildren<TActor extends ProvidedActor> =
export type StateSchema = {
id?: string;
states?: Record<string, StateSchema>;

// Other types
// Needed because TS treats objects with all optional properties as a "weak" object
// https://github.com/statelyai/xstate/issues/5031
type?: unknown;
invoke?: unknown;
on?: unknown;
entry?: unknown;
exit?: unknown;
onDone?: unknown;
after?: unknown;
always?: unknown;
meta?: unknown;
output?: unknown;
tags?: unknown;
description?: unknown;
};

export type StateId<
Expand Down
63 changes: 63 additions & 0 deletions packages/core/test/setup.types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import {
and,
assign,
cancel,
ContextFrom,
createActor,
createMachine,
enqueueActions,
EventFrom,
EventObject,
fromPromise,
fromTransition,
log,
Expand Down Expand Up @@ -2130,4 +2133,64 @@ describe('setup()', () => {
}
});
});

it('EventFrom should work with a machine that has transitions defined on a state', () => {
// https://github.com/statelyai/xstate/issues/5031

const machine = setup({
types: {} as {
events: {
type: 'SOME_EVENT';
};
}
}).createMachine({
id: 'authorization',
initial: 'loading',
context: {
myVar: 'foo'
},
states: {
loaded: {},
loading: {
on: {
SOME_EVENT: {
target: 'loaded'
}
}
}
}
});

((_accept: EventFrom<typeof machine>) => {})({ type: 'SOME_EVENT' });
});

it('ContextFrom should work with a machine that has transitions defined on a state', () => {
// https://github.com/statelyai/xstate/issues/5031

const machine = setup({
types: {} as {
context: {
myVar: string;
};
}
}).createMachine({
id: 'authorization',
initial: 'loading',
context: {
myVar: 'foo'
},
states: {
loaded: {},
loading: {
on: {
SOME_EVENT: {
target: 'loaded'
}
}
}
}
});

((_accept: ContextFrom<typeof machine>) => {})({ myVar: 'whatever' });
});
});

0 comments on commit 7bed484

Please sign in to comment.