useParams and useSearch with Full Inference
What you'll learn: How TanStack Router's hooks automatically infer exact parameter and search types from your route definitions, giving you compile-time safety and IDE autocomplete.
The Type Safety Payoff
You've defined routes with typed path parameters and validated search params in TanStack Router. Now comes the beautiful part: when you use useParams() and useSearch() in your components, TypeScript automatically knows exactly what shape your data has—no manual type annotations needed.
Think of it like a contract: you declared what data your route accepts, and now every component reading that data gets the contract enforced automatically. No more "this might be undefined" or "did I spell that param name right?"
Using useParams with Inference
import { useParams } from '@tanstack/react-router'
function UserProfile() {
// TypeScript knows params.userId is a string (not string | undefined)
// because your route definition said so
const params = useParams({ from: '/users/$userId' })
// Autocomplete works! Type-safe access!
return <div>User ID: {params.userId}</div>
}
The from option tells the hook which route you're reading from, and TypeScript infers the exact shape.
Using useSearch with Inference
function ProductList() {
// TypeScript knows search has the exact shape from your route's validateSearch
const search = useSearch({ from: '/products' })
// If your validator parsed 'page' as a number, it's a number here
// If 'sortBy' had a default, TypeScript knows it's never undefined
return <div>Page {search.page}, sorted by {search.sortBy}</div>
}
No casting, no optional chaining where it shouldn't be needed—just pure type inference from definition to usage.
Key Takeaway: TanStack Router's useParams() and useSearch() hooks infer exact types from your route tree, giving you end-to-end type safety without manual annotations—the router's configuration becomes your single source of truth.