diff --git a/src/guide/extras/web-components.md b/src/guide/extras/web-components.md index 038de5b60e..98230b97be 100644 --- a/src/guide/extras/web-components.md +++ b/src/guide/extras/web-components.md @@ -224,6 +224,29 @@ export function register() { If you have many components, you can also leverage build tool features such as Vite's [glob import](https://vitejs.dev/guide/features.html#glob-import) or webpack's [`require.context`](https://webpack.js.org/guides/dependency-management/#requirecontext) to load all components from a directory. +### Web Components and Typescript {#web-components-and-typescript} + +If you are developing an application or a library, you may want to [type check](/guide/scaling-up/tooling.html#typescript) your Vue components, including those that are defined as custom elements. + +Custom elements are registered globally using native APIs, so by default they won't have type inference when used in Vue templates. To provide type support for Vue components registered as custom elements, we can register global component typings using the the [`GlobalComponents` interface](https://github.com/vuejs/language-tools/blob/master/packages/vscode-vue/README.md#usage) in Vue templates and/or in [JSX](https://www.typescriptlang.org/docs/handbook/jsx.html#intrinsic-elements): + +```typescript +import { defineCustomElement } from 'vue' + +// vue SFC +import CounterSFC from './src/components/counter.ce.vue' + +// turn component into web components +export const Counter = defineCustomElement(CounterSFC) + +// register global typings +declare module 'vue' { + export interface GlobalComponents { + 'Counter': typeof Counter, + } +} +``` + ## Web Components vs. Vue Components {#web-components-vs-vue-components} Some developers believe that framework-proprietary component models should be avoided, and that exclusively using Custom Elements makes an application "future-proof". Here we will try to explain why we believe that this is an overly simplistic take on the problem.