typescript - Hur man utökar `ElementAttrs`-gränssnittet i Vue?


Jag använder Vue 3 med TSX, hur kan jag utöka ElementAttrs<HTMLAttributes>-gränssnittet, så att jag kan göra följande utan att få typfel?

import { defineComponent } from 'vue'

export default defineComponent({
  setup() {
    //                                👇 ts complains
    return () => <div class='v-table' vLoading={true}></div>
  },
})

Felet jag fick är:

Type '{ class: string; vLoading: boolean; }' is not assignable to type 'ElementAttrs<HTMLAttributes>'.

Property 'vLoading' does not exist on type 'ElementAttrs<HTMLAttributes>'.ts(2322)

Svar #1

Jag har listat ut det här:

// Import an entire module for side effects only, without importing anything.
// This runs the module's global code but doesn't actually import any values.
import 'vue'

declare module 'vue' {
  interface HTMLAttributes {
    vLoading?: unknown
  }
}

Relaterade artiklar