Typescript - Fix for ParsedURLQuery, is this dumb?

Following along with https://create.t3.gg/en/usage/trpc I was making a route to query and getting this error over and over. It kept telling me to make it a string, My solution?
const userQuery = trpc.server.getUniqueServer.useQuery(`${query.pid}`);
const userQuery = trpc.server.getUniqueServer.useQuery(`${query.pid}`);
Is this incorrect or bad practice? Both are functional but I personally do not understand why it was erroring.
2 Replies
Neto
Neto2y ago
not the best on first render the query.pid isnt defined when is defined, you cant know if its a string or string[]
const { isReady, query } = useRouter()

const userQuery = trpc.server.getUniqueServer.useQuery(query.pid as string, {
enabled: isReady && typeof query['pid'] === 'string'
});
const { isReady, query } = useRouter()

const userQuery = trpc.server.getUniqueServer.useQuery(query.pid as string, {
enabled: isReady && typeof query['pid'] === 'string'
});
a more safe approach
Debaucus
Debaucus2y ago
Thank you!
Want results from more Discord servers?
Add your server