From 6873d8d0df8faa5d611d0ff6c0ddb2ce4c2dd4d2 Mon Sep 17 00:00:00 2001 From: cexbrayat Date: Mon, 4 May 2020 17:15:46 +0200 Subject: [PATCH] chore(config): proper typings for plugins Adds typings to plugins, still in the quest for switching the TS config to `strict` --- src/config.ts | 29 ++++++++++++++++++++--------- tests/features/plugins.spec.ts | 2 +- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/config.ts b/src/config.ts index c06f6a4b1..4e0bc32f7 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,4 +1,6 @@ import { GlobalMountOptions } from './types' +import { VueWrapper } from './vue-wrapper' +import { ComponentPublicInstance } from 'vue' interface GlobalConfigOptions { global: GlobalMountOptions @@ -9,13 +11,22 @@ interface GlobalConfigOptions { renderStubDefaultSlot: boolean } +interface Plugin { + handler: ( + instance: VueWrapper + ) => Record + options: Record +} + class Pluggable { - installedPlugins: any - constructor() { - this.installedPlugins = [] - } + installedPlugins = [] as Array - install(handler, options = {}) { + install( + handler: ( + instance: VueWrapper + ) => Record, + options: Record = {} + ) { if (typeof handler !== 'function') { console.error('plugin.install must receive a function') handler = () => ({}) @@ -23,13 +34,13 @@ class Pluggable { this.installedPlugins.push({ handler, options }) } - extend(instance) { - const invokeSetup = (plugin) => plugin.handler(instance) // invoke the setup method passed to install + extend(instance: VueWrapper) { + const invokeSetup = (plugin: Plugin) => plugin.handler(instance) // invoke the setup method passed to install const bindProperty = ([property, value]: [string, any]) => { - instance[property] = + ;(instance as any)[property] = typeof value === 'function' ? value.bind(instance) : value } - const addAllPropertiesFromSetup = (setupResult) => { + const addAllPropertiesFromSetup = (setupResult: Record) => { setupResult = typeof setupResult === 'object' ? setupResult : {} Object.entries(setupResult).forEach(bindProperty) } diff --git a/tests/features/plugins.spec.ts b/tests/features/plugins.spec.ts index 76645ed81..ab8d25528 100644 --- a/tests/features/plugins.spec.ts +++ b/tests/features/plugins.spec.ts @@ -69,7 +69,7 @@ describe('Plugin', () => { it.each(plugins)( 'Calling install with %p is handled gracefully', (plugin) => { - config.plugins.VueWrapper.install(plugin) + config.plugins.VueWrapper.install(plugin as any) expect(() => mountComponent()).not.toThrow() } )