-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Closed
Labels
Description
What problem does this feature solve?
Robust prop definition. See the following pic.
What does the proposed API look like?
I have not come up with it.
However in react it does work.
I tried functional component, it doesn't work either.
michalpaukert, PeterParker, BrianHorvat, jd-solanki, calebjacob and 45 more
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
Select code repository
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.
14 remaining items