You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Correct and improve docs on type definitions for Custom Elements.
The doc was incorrect for two reasons:
- For Vue-based custom elements, the type passed into `GlobalComponents` needs to be the Vue component type, not the custom element type, or the custom elements will not be type checked in any Vue templates.
- The name of the elements need to have at least two words and a hyphen, otherwise with single words the custom elements will not be type checked in any Vue templates.
Copy file name to clipboardExpand all lines: src/guide/extras/web-components.md
+56-8
Original file line number
Diff line number
Diff line change
@@ -242,25 +242,73 @@ If you have many components, you can also leverage build tool features such as V
242
242
243
243
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.
244
244
245
-
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):
245
+
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 by augmenting the [`GlobalComponents` interface](https://github.com/vuejs/language-tools/blob/master/packages/vscode-vue/README.md#usage) for type checking in Vue templates (JSX users can augment the [JSX.IntrinsicElements](https://www.typescriptlang.org/docs/handbook/jsx.html#intrinsic-elements) type, which is not shown here).
246
+
247
+
Here is how to define the type for a custom element made with Vue:
A current limitation of this approach for non-Vue custom elements is that event types are not included, for example props like `@some-event="..."` are currently all typed as `(event: Event) => void` without more specific even types. An `Event` can be downcasted into a more specific type as needed.
292
+
293
+
If an element does not have type definitions (for example when they are plain JS and the types are not inferrable by TypeScript), the types of the properties can be defined manually:
294
+
295
+
```ts
296
+
import {SomeElement} from'some-lib'
297
+
importtype {Component} from'vue'
298
+
299
+
// Some Custom Element libraries pre-define their elements. But if they require you to do it instead, remember to do that:
Custom Element library authors can include one or more `.d.ts` files that their users can import, which contain the augmentation so that users don't have to do it themselves. For example, the previous sample could be in a file named `SomeElement.vue-types.d.ts` which is sibling to the other files that the library ships, for Vue users to import in addition to the main file that defines the custom element class.
311
+
264
312
## Web Components vs. Vue Components {#web-components-vs-vue-components}
265
313
266
314
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.
0 commit comments