I'm facing a challenge with my Vue3 component that interacts with GraphQL requests. After receiving a large JSON response, I utilize a computed property to extract the necessary value. Now, I aim to pass this extracted value as a prop to a child component.
The issue arises in the child component where I need to specify the type for my defineProps
call. To accommodate the computed value, it's crucial to have the correct type definition within the child component. The computed property itself is extensive, making manual typing outside of the component undesirable. Instead, I opt to leverage the typeof
operator.
My question is, how can I export this type effectively?
// Parent.vue
<script setup lang="ts">
const { results } = useQuery(...)
const myComputed = computed(() => results.nested.item.is.here)
</script>
<template>
<Child :passToMe="myComputed" />
</template>
// Child.vue
const props = defineProps<
passToMe: ???
>()
I've come across the option of exporting from a separate <script>
tag within the component. However, this method doesn't provide access to the computed property defined in my <script setup>
.
While considering utilizing the type of the GraphQL results, the nested structure poses a complication in implementation.
Transferring the query operation to the Child component solely for type extraction isn't feasible, as other components also rely on the <Child>
component and the specific type enforced by the computed property in the <Parent>
. This minimum type requirement is essential in maintaining consistency.
Given these constraints, how can I efficiently export this type for multiple implementations?