Skip to content

Commit 314d638

Browse files
feat(overmind-vue): add typing support for connect
1 parent 9432821 commit 314d638

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { JSDOM } from 'jsdom'
22
import { Overmind } from 'overmind'
33

44
import { createConnect } from './'
5+
56
const Vue = require('vue/dist/vue')
67

78
const dom = new JSDOM()

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

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { EventType, MODE_SSR } from 'overmind'
1+
import { EventType, IConfiguration, MODE_SSR, Overmind } from 'overmind'
2+
import { Component, ComponentOptions } from 'vue'
3+
4+
type AnyComponent = ComponentOptions<any, any, any> | Component
25

36
const OVERMIND = Symbol('OVERMIND')
47
const IS_PRODUCTION = process.env.NODE_ENV === 'production'
@@ -143,21 +146,25 @@ export const createPlugin = (overmind) => ({
143146
},
144147
})
145148

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]
149149

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-
}
155150

156-
return {
157-
...options,
158-
mixins: (options.mixins ? options.mixins : []).concat(
159-
createMixin(overmind, propsCallback, true)
160-
),
161-
overmind,
162-
} as any
151+
export function createConnect<Config extends IConfiguration>(overmind: Overmind<Config>) {
152+
return <T extends {}>(cb: ((overmind: Overmind<Config>) => T) | AnyComponent, component?: AnyComponent) => {
153+
let options: any = component || cb
154+
let propsCallback = component ? cb : null
155+
156+
if (propsCallback && typeof propsCallback !== 'function') {
157+
throw new Error(
158+
`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`
159+
)
160+
}
161+
162+
return {
163+
...options,
164+
mixins: (options.mixins ? options.mixins : []).concat(
165+
createMixin(overmind, propsCallback, true)
166+
),
167+
overmind,
168+
} as any
169+
}
163170
}

0 commit comments

Comments
 (0)