Skip to content

Commit

Permalink
Passed state type generic to the Config interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Chance Strickland committed Apr 10, 2020
1 parent 7f08309 commit dcbd72f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-lemons-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xstate/fsm': patch
---

Passed state type generic to the Config interface
2 changes: 1 addition & 1 deletion packages/xstate-fsm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function createMachine<
TEvent extends EventObject = EventObject,
TState extends Typestate<TContext> = any
>(
fsmConfig: StateMachine.Config<TContext, TEvent>,
fsmConfig: StateMachine.Config<TContext, TEvent, TState>,
options: {
actions?: StateMachine.ActionMap<TContext, TEvent>;
} = {}
Expand Down
8 changes: 6 additions & 2 deletions packages/xstate-fsm/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,16 @@ export namespace StateMachine {
) => this is TState extends { value: TSV } ? TState : never;
}

export interface Config<TContext extends object, TEvent extends EventObject> {
export interface Config<
TContext extends object,
TEvent extends EventObject,
TState extends Typestate<TContext> = any
> {
id?: string;
initial: string;
context?: TContext;
states: {
[key: string]: {
[key in TState['value']]: {
on?: {
[K in TEvent['type']]?: SingleOrArray<
Transition<TContext, TEvent extends { type: K } ? TEvent : never>
Expand Down
10 changes: 9 additions & 1 deletion packages/xstate-fsm/test/fsm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@ describe('@xstate/fsm', () => {
| {
value: 'yellow';
context: LightContext & { go: false };
}
| {
value: 'red';
context: LightContext & { go: false };
};

const lightConfig: StateMachine.Config<LightContext, LightEvent> = {
const lightConfig: StateMachine.Config<
LightContext,
LightEvent,
LightState
> = {
id: 'light',
initial: 'green',
context: { count: 0, foo: 'bar', go: true },
Expand Down

0 comments on commit dcbd72f

Please sign in to comment.