Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(types): infer options in plugins #282

Merged
merged 2 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ interface GlobalConfigOptions {
renderStubDefaultSlot: boolean
}

interface Plugin<Instance> {
handler: (
instance: Instance,
options: Record<string, any>
) => Record<string, any>
options: Record<string, any>
interface Plugin<Instance, O> {
handler(instance: Instance): Record<string, any>
handler(instance: Instance, options: O): Record<string, any>
options: O
}

class Pluggable<Instance = DOMWrapper<Element>> {
installedPlugins: Plugin<Instance>[] = []
installedPlugins: Plugin<Instance, any>[] = []

install(
handler: (
instance: Instance,
options?: Record<string, any>
) => Record<string, any>,
options: Record<string, any> = {}
install<O>(handler: (instance: Instance) => Record<string, any>)
install<O>(
handler: (instance: Instance, options: O) => Record<string, any>,
options: O
)
install<O>(
handler: (instance: Instance, options?: O) => Record<string, any>,
options?: O
) {
if (typeof handler !== 'function') {
console.error('plugin.install must receive a function')
Expand All @@ -38,7 +38,7 @@ class Pluggable<Instance = DOMWrapper<Element>> {
}

extend(instance: Instance) {
const invokeSetup = ({ handler, options }: Plugin<Instance>) => {
const invokeSetup = ({ handler, options }: Plugin<Instance, any>) => {
return handler(instance, options) // invoke the setup method passed to install
}
const bindProperty = ([property, value]: [string, any]) => {
Expand Down
46 changes: 46 additions & 0 deletions test-dts/plugins.d-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { expectError, expectType } from 'tsd'
import { ComponentPublicInstance } from 'vue'
import { config, VueWrapper } from '../src'

interface OptionsI {
msg: string
}

function PluginWithOptions(
wrapper: VueWrapper<ComponentPublicInstance>,
options: OptionsI
) {
return {}
}

function PluginWithoutOptions(wrapper: VueWrapper<ComponentPublicInstance>) {
return {}
}

function PluginWithOptionalOptions(
wrapper: VueWrapper<ComponentPublicInstance>,
options: OptionsI = { msg: 'hi' }
) {
return {}
}

function PluginWithOptionalOptions2(
wrapper: VueWrapper<ComponentPublicInstance>,
options?: OptionsI
) {
return {}
}

config.plugins.VueWrapper.install(PluginWithOptions, { msg: 'Hello' })
config.plugins.VueWrapper.install(PluginWithoutOptions)
config.plugins.VueWrapper.install(PluginWithOptionalOptions)
config.plugins.VueWrapper.install(PluginWithOptionalOptions2)
config.plugins.VueWrapper.install(PluginWithOptionalOptions, { msg: 'hello' })
config.plugins.VueWrapper.install(PluginWithOptionalOptions2, { msg: 'hello' })

// FIXME: not sure if this one is possible
posva marked this conversation as resolved.
Show resolved Hide resolved
// expectError(config.plugins.VueWrapper.install(PluginWithoutOptions, {}))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is fine?
The FIXME needs to be removed before merging.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine too but I thought I might have overlooked a way of doing it in TS. If there is nothing to fix, yeah, better remove it 😆

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably do something, but I'm not sure it's worth the time, the hassle and the maintenance cost 😉 Thanks for the PR!

expectError(config.plugins.VueWrapper.install(PluginWithOptions, { msg: true }))
expectError(config.plugins.VueWrapper.install(PluginWithOptions, {}))
expectError(config.plugins.VueWrapper.install(PluginWithOptionalOptions, {}))
expectError(config.plugins.VueWrapper.install(PluginWithOptionalOptions2, {}))