Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to infer type when passed as prop to a component? #161

Closed
charlie-s opened this issue Mar 13, 2024 · 1 comment
Closed

How to infer type when passed as prop to a component? #161

charlie-s opened this issue Mar 13, 2024 · 1 comment

Comments

@charlie-s
Copy link

Given this page.vue:

const { data: foo } = await $client.foo.get.useQuery({ id: Number(route.params.id) })
...
<SomeComponent :foo />

what is the recommended approach to inferring the type in SomeComponent?

I tried the following:

server/trpc/routers/index.ts:

import { router } from '../trpc'
import type { inferRouterOutputs } from '@trpc/server'

import { fooRouter } from './foo'

export const appRouter = router({
  foo: fooRouter
})

export type AppRouter = typeof appRouter
export type RouterOutput = inferRouterOutputs

components/SomeComponent.vue:

import { type RouterOutput } from 'server/trpc/routers'
defineProps<{foo: RouterOutput['foo']['get']}>()

but this results in the error A composable that requires access to the Nuxt instance was called outside of a plugin, Nuxt hook, Nuxt middleware, or Vue setup function..

Before I go deeper down the rabbit hole, I wonder if there's something I couldn't find in the docs that help with this.

@Jimmyzm
Copy link
Contributor

Jimmyzm commented Mar 15, 2024

You should pass the AppRouter type to the inferRouterOutputs

export type AppRouter = typeof appRouter
export type RouterOutput = inferRouterOutputs<AppRouter>  <== this line

And you'd better write the code somewhere client side then import.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants