Skip to content

Commit 198981a

Browse files
fix(overmind): base state typing fix
1 parent 0750df5 commit 198981a

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

packages/node_modules/overmind/src/statemachine.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import { IState } from '.'
55

66
type TState = {
77
current: string
8-
} & {
8+
} & TBaseState
9+
10+
type TBaseState = {
911
[key: string]: IState | Statemachine<any, any, any>
1012
}
1113

@@ -15,13 +17,13 @@ type TEvents = {
1517
}
1618

1719

18-
export type StatemachineTransitions<States extends TState, Events extends TEvents, BaseState extends IState> = {
20+
export type StatemachineTransitions<States extends TState, Events extends TEvents, BaseState extends TBaseState> = {
1921
[Type in Events["type"]]: [BaseState] extends [never] ?
2022
((state: States, payload: Events extends { type: Type } ? Events["data"] : never) => States | void) :
2123
((state: States & BaseState, payload: Events extends { type: Type } ? Events["data"] : never) => States | void)
2224
}
2325

24-
export interface MachineMethods<States extends TState, Events extends TEvents, BaseState extends IState> {
26+
export interface MachineMethods<States extends TState, Events extends TEvents, BaseState extends TBaseState> {
2527
matches<T extends States["current"]>(
2628
current: T,
2729
): Statemachine<States extends { current: T} ? States : never, Events, BaseState> | undefined
@@ -30,7 +32,7 @@ export interface MachineMethods<States extends TState, Events extends TEvents, B
3032
): Statemachine<States, Events, BaseState>
3133
}
3234

33-
export type Statemachine<States extends TState, Events extends TEvents, BaseState extends IState = never> = States & BaseState & MachineMethods<States, Events, BaseState>
35+
export type Statemachine<States extends TState, Events extends TEvents, BaseState extends TBaseState = never> = States & BaseState & MachineMethods<States, Events, BaseState>
3436

3537
const INITIAL_STATE = Symbol('INITIAL_STATE')
3638
const TRANSITIONS = Symbol('TRANSITIONS')
@@ -39,7 +41,7 @@ const IS_DISPOSED = Symbol('IS_DISPOSED')
3941
const CURRENT_KEYS = Symbol('CURRENT_KEYS')
4042
const BASE_STATE = Symbol('BASE_STATE')
4143

42-
export class StateMachine<State extends TState, Events extends TEvents, BaseState extends IState> {
44+
export class StateMachine<State extends TState, Events extends TEvents, BaseState extends TBaseState> {
4345
current: State["current"]
4446
private [INITIAL_STATE]: State["current"]
4547
private [TRANSITIONS]: StatemachineTransitions<State, Events, BaseState>
@@ -101,13 +103,13 @@ export class StateMachine<State extends TState, Events extends TEvents, BaseStat
101103
}
102104
}
103105

104-
export type StatemachineFactory<States extends TState, Events extends TEvents, BaseState extends IState> = [BaseState] extends [never] ? {
106+
export type StatemachineFactory<States extends TState, Events extends TEvents, BaseState extends TBaseState> = [BaseState] extends [never] ? {
105107
create(state: States): Statemachine<States, Events, {}>
106108
} : {
107109
create(state: States, baseState: BaseState): Statemachine<States, Events, BaseState>
108110
}
109111

110-
export function statemachine<States extends TState, Events extends TEvents, BaseState extends IState = never>(transitions: StatemachineTransitions<States, Events, BaseState>): StatemachineFactory<States, Events, BaseState> {
112+
export function statemachine<States extends TState, Events extends TEvents, BaseState extends TBaseState = never>(transitions: StatemachineTransitions<States, Events, BaseState>): StatemachineFactory<States, Events, BaseState> {
111113
return {
112114
create(state, baseState) {
113115
return new StateMachine(transitions, state as any, baseState as any)

0 commit comments

Comments
 (0)