LinusBorg / portal-vue

A feature-rich Portal Plugin for Vue 3, for rendering DOM outside of a component, anywhere in your app or the entire document. (Vue 2 version: v2.portal-vue.linusb.org)
http://portal-vue.linusb.org
MIT License
3.89k stars 188 forks source link

[Feature Request] Add TypeScript support #380

Closed kingyue737 closed 6 months ago

kingyue737 commented 2 years ago

Currently I need to add shims in all my projects to get intellisense and typecheck in template (powered by Volar):

import type { DefineComponent } from 'vue'

declare module 'vue' {
  export interface GlobalComponents {
    Portal: DefineComponent<{
      disabled?: boolean
      name?: string
      order?: number
      slim?: boolean
      slotProps?: any
      tag?: string
      to: string
    }>
    PortalTarget: DefineComponent<{
      multiple?: boolean
      name: string
      slim?: boolean
      slotProps?: any
      tag?: string
      transition?: boolean | string | object
    }>
  }
}

export {}

Autocomplete and typecheck: image

Is it possible to add built-in support in this package? However, only Vue 2.7 and @vue/runtime-dom have DefineComponent.

dschmidt commented 1 year ago

See also: https://vuejs.org/guide/typescript/options-api.html#augmenting-custom-options "Note this is just an example - well-typed libraries like vue-router should automatically perform these augmentations in their own type definitions." This is about ComponentCustomOptions but the same applies to GlobalComponents.

Until then (depending on the build tooling) an even easier approach to set it up in downstream projects is the following:

// file needs to be an actual module, so we need at least one import or export
export {}

declare module 'vue' {
  interface GlobalComponents {
    Portal: typeof import('portal-vue')['Portal']
    PortalTarget: typeof import('portal-vue')['PortalTarget']
  }
}
kingyue737 commented 6 months ago

Closing as Vue 2 has reached EOL