Skip to content

Commit 703efdb

Browse files
fix(overmind-vue): fix typing of hooks
1 parent e6e68ca commit 703efdb

File tree

1 file changed

+36
-33
lines changed

1 file changed

+36
-33
lines changed

packages/overmind-vue/src/vue3.ts

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
IConfiguration,
55
MODE_SSR,
66
Overmind,
7+
IContext,
78
} from 'overmind'
89
import {
910
Ref,
@@ -37,20 +38,20 @@ export const withOvermind = (
3738
})
3839
}
3940

40-
export interface StateHook<Config extends IConfiguration> {
41-
(): Ref<Overmind<Config>['state']>
42-
<CB extends (state: Overmind<Config>['state']) => object>(
43-
cb: CB
44-
): CB extends (state: Overmind<Config>['state']) => infer O
41+
export interface StateHook<Context extends IContext<{ state: {} }>> {
42+
(): Ref<Context['state']>
43+
<CB extends (state: Context['state']) => object>(cb: CB): CB extends (
44+
state: Context['state']
45+
) => infer O
4546
? O extends object
4647
? Ref<O>
4748
: never
4849
: never
4950
}
5051

51-
export function createStateHook<Config extends IConfiguration>(): StateHook<
52-
Config
53-
> {
52+
export function createStateHook<
53+
Context extends IContext<{ state: {} }>
54+
>(): StateHook<Context> {
5455
const componentId = nextComponentId++
5556
let componentInstanceId = 0
5657
return ((cb: any) => {
@@ -138,61 +139,63 @@ export function createStateHook<Config extends IConfiguration>(): StateHook<
138139
}) as any
139140
}
140141

141-
export interface ActionsHook<Config extends IConfiguration> {
142-
(): Ref<Overmind<Config>['actions']>
143-
<CB extends (actions: Overmind<Config>['actions']) => object>(
144-
cb: CB
145-
): CB extends (actions: Overmind<Config>['actions']) => infer O
142+
export interface ActionsHook<Context extends IContext<{ actions: {} }>> {
143+
(): Ref<Context['actions']>
144+
<CB extends (actions: Context['actions']) => object>(cb: CB): CB extends (
145+
actions: Context['actions']
146+
) => infer O
146147
? O extends object
147148
? Ref<O>
148149
: never
149150
: never
150151
}
151152

152-
export function createActionsHook<Config extends IConfiguration>(): ActionsHook<
153-
Config
154-
> {
155-
return ((cb?: any): Overmind<Config>['actions'] => {
153+
export function createActionsHook<
154+
Context extends IContext<{ actions: {} }>
155+
>(): ActionsHook<Context> {
156+
return ((cb?: any): Context['actions'] => {
156157
const overmindInstance = inject<any>('overmind')
157158

158159
return cb ? cb(overmindInstance.actions) : overmindInstance.actions
159160
}) as any
160161
}
161162

162-
export interface EffectsHook<Config extends IConfiguration> {
163-
(): Ref<Overmind<Config>['effects']>
164-
<CB extends (effects: Overmind<Config>['effects']) => object>(
165-
cb: CB
166-
): CB extends (effects: Overmind<Config>['effects']) => infer O
163+
export interface EffectsHook<Context extends IContext<{ effects: {} }>> {
164+
(): Ref<Context['effects']>
165+
<CB extends (effects: Context['effects']) => object>(cb: CB): CB extends (
166+
effects: Context['effects']
167+
) => infer O
167168
? O extends object
168169
? Ref<O>
169170
: never
170171
: never
171172
}
172173

173-
export function createEffectsHook<Config extends IConfiguration>(): EffectsHook<
174-
Config
175-
> {
176-
return ((cb?: any): Overmind<Config>['effects'] => {
174+
export function createEffectsHook<
175+
Context extends IContext<{ effects: {} }>
176+
>(): EffectsHook<Context> {
177+
return ((cb?: any): Context['effects'] => {
177178
const overmindInstance = inject<any>('overmind')
178179

179180
return cb ? cb(overmindInstance.effects) : overmindInstance.effects
180181
}) as any
181182
}
182183

183-
export function createReactionHook<Config extends IConfiguration>() {
184-
return (): Overmind<Config>['reaction'] => {
184+
export function createReactionHook<Context extends IContext<{ state: {} }>>() {
185+
return (): Context['reaction'] => {
185186
const overmindInstance = inject<any>('overmind')
186187

187188
return overmindInstance.reaction
188189
}
189190
}
190191

191-
export function createHooks<Config extends IConfiguration>() {
192+
export function createHooks<
193+
Context extends IContext<{ state: {}; actions: {}; effects: {} }>
194+
>() {
192195
return {
193-
state: createStateHook<Config>(),
194-
actions: createActionsHook<Config>(),
195-
effects: createEffectsHook<Config>(),
196-
reaction: createReactionHook<Config>(),
196+
state: createStateHook<Context>(),
197+
actions: createActionsHook<Context>(),
198+
effects: createEffectsHook<Context>(),
199+
reaction: createReactionHook<Context>(),
197200
}
198201
}

0 commit comments

Comments
 (0)