Skip to content

Commit 0408888

Browse files
authored
Add note about how to reference generic components (#2945)
1 parent 327acb6 commit 0408888

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Diff for: src/api/sfc-script-setup.md

+19
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,25 @@ defineProps<{
469469
</script>
470470
```
471471

472+
In order to use a reference to a generic component in a `ref` you need to use the [`vue-component-type-helpers`](https://www.npmjs.com/package/vue-component-type-helpers) library as `InstanceType` won't work.
473+
474+
```vue
475+
<script
476+
setup
477+
lang="ts"
478+
>
479+
import componentWithoutGenerics from '../component-without-generics.vue';
480+
import genericComponent from '../generic-component.vue';
481+
482+
import type { ComponentExposed } from 'vue-component-type-helpers';
483+
484+
// Works for a component without generics
485+
ref<InstanceType<typeof componentWithoutGenerics>>();
486+
487+
ref<ComponentExposed<typeof genericComponent>>();
488+
```
489+
490+
472491
## Restrictions {#restrictions}
473492

474493
- Due to the difference in module execution semantics, code inside `<script setup>` relies on the context of an SFC. When moved into external `.js` or `.ts` files, it may lead to confusion for both developers and tools. Therefore, **`<script setup>`** cannot be used with the `src` attribute.

0 commit comments

Comments
 (0)