Can I refer to the other prop inside a prop? #143
-
Can I do something like this using Volar, and get the auto-completion and type checking support?
|
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 12 replies
This comment has been hidden.
This comment has been hidden.
-
Also has a type-only way, it should be support reuse the child component. A challenge is how to define // child.vue
import { defineComponent, PropType } from 'vue'
interface MyObject {
[key: string]: any
}
export default defineComponent({
props: {
value: Object as PropType<MyObject>,
key: String as PropType<keyof MyObject>
},
}) as ChildType<MyObject>;
export type ChildType<T extends MyObject> = DefineComponent<{
value: PropType<T>,
key: PropType<keyof T>,
}>; // parent.vue
import { defineComponent } from 'vue'
import Child, { ChildType } from './child.vue'
export default defineComponent({
components: {
Child: Child as ChildType<{ foo: string, bar: number }>
}
}) |
Beta Was this translation helpful? Give feedback.
-
Hello again! I found myself using new script setup sugar (which is still an rfc, I know) a lot, and I was wondering if I can get the same type checking and autocomplete using script setup syntax? Even with some hacks? |
Beta Was this translation helpful? Give feedback.
-
Good day, Johnson! ! It's me again! (haha) This approach works really well with props, but I lost all my slot's types. Can I fix this somehow? And also include generics inside them? Or is this a Volar specific feature and there is nothing I can do? |
Beta Was this translation helpful? Give feedback.
-
Generic functional component already supported in v0.25.2. // child.vue
<script lang="ts">
import { h } from "vue";
const Comp = <T extends object>(props: {
items: T;
keys?: keyof T;
}) => h({}, props);
export default Comp;
</script> <template>
<Child :items="{ foo: '' }" keys="foo" />
<Child :items="{ foo: '' }" keys="bar" /> <!-- Type '"bar"' is not assignable to type '"foo" | undefined' -->
</template>
<script lang="ts" setup>
import Child from './child.vue'
</script> |
Beta Was this translation helpful? Give feedback.
-
Thanks for the help.
|
Beta Was this translation helpful? Give feedback.
Also has a type-only way, it should be support reuse the child component. A challenge is how to define
DefineComponent<...>
correctly.