installHelp.ts 694 Bytes
import type { App, Component, FunctionalComponent, Plugin } from 'vue'

interface EventShim {
  new (...args: any[]): {
    $props: {
      onClick?: (...args: any[]) => void
    }
  }
}

export type WithInstall<T> = T & {
  install(app: App): void
} & EventShim

export const withInstall = <T extends Component | FunctionalComponent>(component: T, alias?: string) => {
  (component as Plugin).install = (app: App) => {
    const componentName = component.name || (component as FunctionalComponent).displayName
    if (!componentName) return
    app.component(componentName, component)

    if (alias) app.config.globalProperties[alias] = component
    return component as WithInstall<T>
  }
}