Skip to content

Commit 6873d8d

Browse files
committed
chore(config): proper typings for plugins
Adds typings to plugins, still in the quest for switching the TS config to `strict`
1 parent 656adc2 commit 6873d8d

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

src/config.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { GlobalMountOptions } from './types'
2+
import { VueWrapper } from './vue-wrapper'
3+
import { ComponentPublicInstance } from 'vue'
24

35
interface GlobalConfigOptions {
46
global: GlobalMountOptions
@@ -9,27 +11,36 @@ interface GlobalConfigOptions {
911
renderStubDefaultSlot: boolean
1012
}
1113

14+
interface Plugin {
15+
handler: (
16+
instance: VueWrapper<ComponentPublicInstance>
17+
) => Record<string, any>
18+
options: Record<string, any>
19+
}
20+
1221
class Pluggable {
13-
installedPlugins: any
14-
constructor() {
15-
this.installedPlugins = []
16-
}
22+
installedPlugins = [] as Array<Plugin>
1723

18-
install(handler, options = {}) {
24+
install(
25+
handler: (
26+
instance: VueWrapper<ComponentPublicInstance>
27+
) => Record<string, any>,
28+
options: Record<string, any> = {}
29+
) {
1930
if (typeof handler !== 'function') {
2031
console.error('plugin.install must receive a function')
2132
handler = () => ({})
2233
}
2334
this.installedPlugins.push({ handler, options })
2435
}
2536

26-
extend(instance) {
27-
const invokeSetup = (plugin) => plugin.handler(instance) // invoke the setup method passed to install
37+
extend(instance: VueWrapper<ComponentPublicInstance>) {
38+
const invokeSetup = (plugin: Plugin) => plugin.handler(instance) // invoke the setup method passed to install
2839
const bindProperty = ([property, value]: [string, any]) => {
29-
instance[property] =
40+
;(instance as any)[property] =
3041
typeof value === 'function' ? value.bind(instance) : value
3142
}
32-
const addAllPropertiesFromSetup = (setupResult) => {
43+
const addAllPropertiesFromSetup = (setupResult: Record<string, any>) => {
3344
setupResult = typeof setupResult === 'object' ? setupResult : {}
3445
Object.entries(setupResult).forEach(bindProperty)
3546
}

tests/features/plugins.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('Plugin', () => {
6969
it.each(plugins)(
7070
'Calling install with %p is handled gracefully',
7171
(plugin) => {
72-
config.plugins.VueWrapper.install(plugin)
72+
config.plugins.VueWrapper.install(plugin as any)
7373
expect(() => mountComponent()).not.toThrow()
7474
}
7575
)

0 commit comments

Comments
 (0)