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
// Add the new element type to Vue's GlobalComponents type.
294
294
declaremodule'vue' {
295
295
interfaceGlobalComponents {
296
-
// Be sure to pass in the Vue component type here (SomeComponent, *not* SomeElement).
297
-
// Custom Elements require a hyphen in their name, so use the hyphenated element name here.
296
+
// Be sure to pass in the Vue component type here
297
+
// (SomeComponent, *not* SomeElement).
298
+
// Custom Elements require a hyphen in their name,
299
+
// so use the hyphenated element name here.
298
300
'some-element':typeofSomeComponent
299
301
}
300
302
}
301
303
```
302
304
303
305
## Non-Vue Web Components and TypeScript {#non-vue-web-components-and-typescript}
304
306
305
-
Here is the recommended way to enable type checking in SFC templates of Custom
306
-
Elements that are not built with Vue.
307
+
Here is the recommended way to enable type checking in SFC templates of Custom Elements that are not built with Vue.
307
308
308
309
> [!Note]
309
310
> This approach is one possible way to do it, but it may vary depending on the
310
311
> framework being used to create the custom elements.
311
312
312
-
Suppose we have a custom element with some JS properties and events defined, and
313
-
it is shipped in a library called `some-lib`:
313
+
Suppose we have a custom element with some JS properties and events defined, and it is shipped in a library called `some-lib`:
314
314
315
315
```ts
316
316
// file: some-lib/src/SomeElement.ts
@@ -348,11 +348,9 @@ export class AppleFellEvent extends Event {
348
348
}
349
349
```
350
350
351
-
The implementation details have been omitted, but the important part is that we
352
-
have type definitions for two things: prop types and event types.
351
+
The implementation details have been omitted, but the important part is that we have type definitions for two things: prop types and event types.
353
352
354
-
Let's create a type helper for easily registering custom element type
355
-
definitions in Vue:
353
+
Let's create a type helper for easily registering custom element type definitions in Vue:
356
354
357
355
```ts
358
356
// file: some-lib/src/DefineCustomElement.ts
@@ -367,15 +365,17 @@ type DefineCustomElement<
367
365
// specifically reads prop definitions from the `$props` type. Note that we
368
366
// combine the element's props with the global HTML props and Vue's special
369
367
// props.
370
-
/**@deprecated Do not use the $props property on a Custom Element ref, this is for template prop types only. */
368
+
/**@deprecated Do not use the $props property on a Custom Element ref,
369
+
this is for template prop types only. */
371
370
$props:HTMLAttributes&
372
371
Partial<Pick<ElementType, SelectedAttributes>> &
373
372
PublicProps
374
373
375
374
// Use $emit to specifically define event types. Vue specifically reads event
376
375
// types from the `$emit` type. Note that `$emit` expects a particular format
377
376
// that we map `Events` to.
378
-
/**@deprecated Do not use the $emit property on a Custom Element ref, this is for template prop types only. */
377
+
/**@deprecated Do not use the $emit property on a Custom Element ref,
378
+
this is for template prop types only. */
379
379
$emit:VueEmit<Events>
380
380
}
381
381
@@ -390,13 +390,12 @@ type VueEmit<T extends EventMap> = EmitFn<{
390
390
```
391
391
392
392
> [!Note]
393
-
> We marked `$props` and `$emit` as deprecated so that when we get a `ref` to a
394
-
> custom element we will not be tempted to use these properties, as these
393
+
> We marked `$props` and `$emit` as deprecated so that when we get a `ref` to
394
+
> a custom element we will not be tempted to use these properties, as these
395
395
> properties are for type checking purposes only when it comes to custom elements.
396
396
> These properties do not actually exist on the custom element instances.
397
397
398
-
Using the type helper we can now select the JS properties that should be exposed
399
-
for type checking in Vue templates:
398
+
Using the type helper we can now select the JS properties that should be exposed for type checking in Vue templates:
400
399
401
400
```ts
402
401
// file: some-lib/src/SomeElement.vue.ts
@@ -421,8 +420,7 @@ declare module 'vue' {
421
420
}
422
421
```
423
422
424
-
Suppose that `some-lib` builds its source TypeScript files into a `dist/` folder. A user of
425
-
`some-lib` can then import `SomeElement` and use it in a Vue SFC like so:
423
+
Suppose that `some-lib` builds its source TypeScript files into a `dist/` folder. A user of `some-lib` can then import `SomeElement` and use it in a Vue SFC like so:
426
424
427
425
```vue
428
426
<script setup lang="ts">
@@ -446,7 +444,8 @@ onMounted(() => {
446
444
el.value!.someMethod()
447
445
)
448
446
449
-
// Do not use these props, they are `undefined` (IDE will show them crossed out):
447
+
// Do not use these props, they are `undefined`
448
+
// IDE will show them crossed out
450
449
el.$props
451
450
el.$emit
452
451
})
@@ -467,8 +466,7 @@ onMounted(() => {
467
466
</template>
468
467
```
469
468
470
-
If an element does not have type definitions, the types of the properties and events can be
471
-
defined in a more manual fashion:
469
+
If an element does not have type definitions, the types of the properties and events can be defined in a more manual fashion:
472
470
473
471
```vue
474
472
<script setup lang="ts">
@@ -503,11 +501,7 @@ declare module 'vue' {
503
501
</template>
504
502
```
505
503
506
-
Custom Element authors should not automatically export framework-specific custom
507
-
element type definitions from their libraries, for example they should not
508
-
export them from an `index.ts` file that also exports the rest of the library,
509
-
otherwise users will have unexpected module augmentation errors. Users should
510
-
import the framework-specific type definition file that they need.
504
+
Custom Element authors should not automatically export framework-specific custom element type definitions from their libraries, for example they should not export them from an `index.ts` file that also exports the rest of the library, otherwise users will have unexpected module augmentation errors. Users should import the framework-specific type definition file that they need.
511
505
512
506
## Web Components vs. Vue Components {#web-components-vs-vue-components}
0 commit comments