Skip to content

Commit 0958cd6

Browse files
feat(overmind-vue): improve typing
1 parent 01000e8 commit 0958cd6

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

packages/node_modules/overmind-vue/src/Index.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ const dom = new JSDOM()
88

99
describe('Vue', () => {
1010
it('should mount with connected app', () => {
11-
const app = new Overmind({
11+
const config = {
1212
state: {
1313
foo: 'bar',
1414
},
15-
})
15+
}
16+
const app = new Overmind(config)
1617
const connect = createConnect(app)
1718
const vm = new Vue(
1819
connect({
@@ -22,11 +23,12 @@ describe('Vue', () => {
2223
expect(vm.overmind.state.foo).toBe('bar')
2324
})
2425
it('should mount with custom connected app', () => {
25-
const app = new Overmind({
26+
const config = {
2627
state: {
2728
foo: 'bar',
2829
},
29-
})
30+
}
31+
const app = new Overmind(config)
3032
const connect = createConnect(app)
3133
const vm = new Vue(
3234
connect(

packages/node_modules/overmind-vue/src/index.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { EventType, MODE_SSR } from 'overmind'
1+
import { Component, ComponentOptions } from 'vue'
2+
import { EventType, MODE_SSR, IConfiguration, Overmind } from 'overmind'
23

34
const OVERMIND = Symbol('OVERMIND')
45
const IS_PRODUCTION = process.env.NODE_ENV === 'production'
@@ -143,21 +144,26 @@ export const createPlugin = (overmind) => ({
143144
},
144145
})
145146

146-
export const createConnect = (overmind) => (...args) => {
147-
let options = args.length === 1 ? args[0] : args[1]
148-
let propsCallback = args.length === 1 ? null : args[0]
149147

150-
if (propsCallback && typeof propsCallback !== 'function') {
151-
throw new Error(
152-
`OVERMIND-VUE: When passing two arguments to "connect", the first has to be a function. You can alternatively only pass a single argument, which is the component`
153-
)
154-
}
155148

156-
return {
157-
...options,
158-
mixins: (options.mixins ? options.mixins : []).concat(
159-
createMixin(overmind, propsCallback, true)
160-
),
161-
overmind,
162-
} as any
163-
}
149+
export function createConnect<T extends Overmind<any>>(overmind: T):
150+
<P extends { [key: string]: any }, C extends ComponentOptions<any>>(propsCallback: ((overmind: T) => P) | C, component?: C) => C {
151+
return (componentOrPropsCallback, maybeComponent) => {
152+
const options = (maybeComponent || componentOrPropsCallback) as ComponentOptions<any>
153+
const propsCallback = maybeComponent ? componentOrPropsCallback : null
154+
155+
if (propsCallback && typeof propsCallback !== 'function') {
156+
throw new Error(
157+
`OVERMIND-VUE: When passing two arguments to "connect", the first has to be a function. You can alternatively only pass a single argument, which is the component`
158+
)
159+
}
160+
161+
return {
162+
...options,
163+
mixins: (options.mixins ? options.mixins : []).concat(
164+
createMixin(overmind, propsCallback, true)
165+
),
166+
overmind,
167+
} as any
168+
}
169+
}

0 commit comments

Comments
 (0)