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
{{ message }}
Repository navigation
Is there any way to use generic when defining props? #3102
I tried functional component, it doesn't work either.
Reacted by Michal Paukert, Peter Parker, Brian Horvat, JD Solanki, Caleb Jacob, Oswaldo Freitas, LifeIsStrange, vanya, Ivan Monastyrev, whale9490 and 40 more
I know vue can do a runtime validation. What I need is to make different prop got connected by generic. For example you can create a select component with props:
But if I do this there would be a lot of problem when handling prop internally and do prop type check. Conceptually the best way is to specify the prop like this
<template>
<divclass="image-gallery">
<!-- ..stuff.. (display images list) -->
<sectionv-if="$scopedSlots['selected-image-viewer']">
<slotname="selected-image-viewer":selected-image="selectedImage"></slot>
</section>
</div>
</template>
<script lang="ts">
import { Image, PhotoImage } from"@/types";import { defineComponent, PropType, Ref, ref } from"@vue/composition-api";exportdefaultdefineComponent({ props: { images: {// currently, this is not possible? And so I just use `PropType<Image>` instead type: PropType<TextendsImage>, default: [], }, setup(props) {const selectedImage:Ref<null|T> =ref(null); // currently `Ref<null | Image>`// ... logic that sets a "selected" image to one of the images on clickreturn {selectedImage, }; },});</script>
PhotosPage.vue
<template>
<divid="my-page">
<ImageGallery :images="images">
<template #selected-image-viewer="{ selectedImage }">
<!-- So here, selectedImage should be of type `PhotoImage`, because we gave `PhotoImage[]`. But, right now it is type Image, and doesn't have the fields of PhotoImage. As a workaround I cast to `any` within the component and expose that for the slot instead (no type safety).-->
</template>
</ImageGallery>
</div>
</template>
<script lang="ts">
import { PhotoImage } from"@/types";import { defineComponent, Ref, ref } from"@vue/composition-api";importImageGalleryfrom"@/components/ImageGallery.vue";exportdefaultdefineComponent({ components: {ImageGallery, }, setup() {const images:Ref<PhotoImage[]> =ref([]);// set images somehowreturn {images, } },});
</script>
Now, use this component can get generic props work, but will lost the scoped slots type.
My idea is to extend the functional component formats like this: add slots to SetupContext, so volar can intellisense both generic props type and scoped slots props type?
I was able to get volar to properly see props/slots, but it required manually defining the Slots and using a wrapper component.
The <NoGenerics> component is implemented normally
Thank you @achaphiv. I used your approach to make my component handle generic properties and it worked perfectly with Volar 0.40.13.
Unfortunately, once I upgraded to Volar 1.0.0, the Vue props no longer inferred their types from the values assigned. When I hover over the prop it now shows MyComponent<unknown>.myProp.
I've had to downgrade Volar back to 0.40.13 for it to work properly in VS Code.
I should note that in Volar 1.0.0, the code still compiles with vue-tsc but it fails with type-checking.
Activity
pikax commentedon Feb 1, 2021
Vue handles props differently from react, in vue a prop can have runtime validation.
I have this #3049 PR to introduce a similar way to pass the type to props, but this will still require you to define the props object.
I might misunderstand your issue, please clarify
07akioni commentedon Feb 1, 2021
I know vue can do a runtime validation. What I need is to make different prop got connected by generic. For example you can create a select component with props:
But if I do this there would be a lot of problem when handling prop internally and do prop type check. Conceptually the best way is to specify the prop like this
React component libraries do a lot like this.
oswaldofreitas commentedon Oct 11, 2021
Is there any other place I can follow discussions/progress about this one?
iliubinskii commentedon Feb 3, 2022
If you only need generic props then you can use this tutorial:
https://logaretm.com/blog/generically-typed-vue-components/
It worked for me
BUT:
It does not help in creating generic slots.
If anyone has solution to create generic component with both generic props and generic slots, please, share your ideas.
07akioni commentedon Feb 3, 2022
I've a hacky workaround (with setup only), it works for tsx, ts, template. However I don't recommend it.
I think it isn't a good idea to implement a generic component before vue officially support it.
agileago commentedon Feb 4, 2022
@07akioni maybe we can use class component + tsx. and it can solve all the pain points. see https://agileago.github.io/vue3-oop/
example
pikax commentedon Feb 17, 2022
We need to use classes to solve this, classes are not planned to be supported.
Another way to do this (hacky overhead), would be doing something like:
playground
iliubinskii commentedon Feb 17, 2022
For anyone interested I found the following solution:
I use GlobalComponentConstructor type from Quasar framework:
// Quasar type type GlobalComponentConstructor<Props = {}, Slots = {}> = { new (): { $props: PublicProps & Props $slots: Slots } } interface MyComponentProps<T> { // Define props here } interface MyComponentSlots<T> { // Define slots here } type MyComponentGeneric<T> = GlobalComponentConstructor<MyComponentProps<T>, MyComponentSlots<T>>; defineComponent({ name: "another-component", components: { "my-component-generic-boolean": MyComponent as unknown as MyComponentGeneric<boolean>, "my-component-generic-string": MyComponent as unknown as MyComponentGeneric<string> } }Volar and vue-tsc recognize the above pattern.
As a result I get type safety both for props and slots.
The downside is that I need to define Slots and Props interfaces.
However, Quasar does the same.
It would be ideal if Vue added defineComponent<Slots, Props> version of defineComponent that would validate my Slots and Props interfaces.
aentwist commentedon Apr 3, 2022
I will add an example of my ideal usage pattern, and explain what I am currently doing to work around this. +1.
types.d.ts
ImageGallery.vue
PhotosPage.vue
ccqgithub commentedon Apr 10, 2022
The solutions above seems all not work with
scopedSlot props(intellisense scopedSlots types by props types)?I found out that now volar can support generics for
functional component, is it possible to usefunctional componentto do this?Now, use this component can get generic props work, but will lost the scoped slots type.
My idea is to extend the functional component formats like this: add slots to
SetupContext, so volar can intellisense bothgeneric props typeandscoped slots props type?achaphiv commentedon Jul 23, 2022
I was able to get volar to properly see props/slots, but it required manually defining the
Slotsand using a wrapper component.The
<NoGenerics>component is implemented normallyAnd then the
<YesGenerics>is the one that gets used in the rest of the project:colinj commentedon Oct 8, 2022
Thank you @achaphiv. I used your approach to make my component handle generic properties and it worked perfectly with Volar 0.40.13.
Unfortunately, once I upgraded to Volar 1.0.0, the Vue props no longer inferred their types from the values assigned. When I hover over the prop it now shows
MyComponent<unknown>.myProp.I've had to downgrade Volar back to 0.40.13 for it to work properly in VS Code.
I should note that in Volar 1.0.0, the code still compiles with
vue-tscbut it fails with type-checking.hymair commentedon Dec 28, 2022
I just want to mention this video which outlines (and solves for React) this exact issue - https://www.youtube.com/watch?v=hBk4nV7q6-w
feat(types): `defineComponent()` with generics support (vuejs#7963)
feat(types): `defineComponent()` with generics support (vuejs#7963)