Skip to content

Commit 8a1da00

Browse files
authored
Add docs on how to reference generic components (#2946)
1 parent 2053725 commit 8a1da00

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/guide/typescript/composition-api.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,3 +423,38 @@ import type { ComponentPublicInstance } from 'vue'
423423

424424
const child = ref<ComponentPublicInstance | null>(null)
425425
```
426+
427+
In cases where the component referenced is a [generic component](/guide/typescript/overview.html#generic-components), for instance `MyGenericModal`:
428+
429+
```vue
430+
<!-- MyGenericModal.vue -->
431+
<script setup lang="ts" generic="ContentType extends string | number">
432+
import { ref } from 'vue'
433+
434+
const content = ref<ContentType | null>(null)
435+
436+
const open = (newContent: ContentType) => (content.value = newContent)
437+
438+
defineExpose({
439+
open
440+
})
441+
</script>
442+
```
443+
444+
It needs to be referenced using `ComponentExposed` from the [`vue-component-type-helpers`](https://www.npmjs.com/package/vue-component-type-helpers) library as `InstanceType` won't work.
445+
446+
```vue
447+
<!-- App.vue -->
448+
<script setup lang="ts">
449+
import MyGenericModal from './MyGenericModal.vue'
450+
451+
import type { ComponentExposed } from 'vue-component-type-helpers';
452+
453+
const modal = ref<ComponentExposed<typeof MyModal> | null>(null)
454+
455+
const openModal = () => {
456+
modal.value?.open('newValue')
457+
}
458+
</script>
459+
```
460+

0 commit comments

Comments
 (0)