Skip to main content

Type Safety

TanStack Router is built with TypeScript from the ground up, providing unparalleled type safety throughout your routing layer. Every navigation, every parameter, and every search param is fully typed.

Why Type Safety Matters

Type-safe routing eliminates entire categories of bugs:
  • No broken links - TypeScript errors if you navigate to a non-existent route
  • No missing parameters - Required params are enforced at compile time
  • No malformed search params - Search parameters are validated and typed
  • Autocomplete everywhere - Full IDE support for paths, params, and more
This means you catch routing errors during development, not in production.

Route Path Types

TanStack Router automatically generates types for all valid route paths.

Type-Safe Navigation

The to property only accepts valid route paths from your route tree.

Path Inference

Path types are inferred from your route tree structure:
These types are generated automatically from the route tree structure defined in packages/router-core/src/routeInfo.ts.

Parameter Type Safety

Path parameters are fully typed based on the route path pattern.

Required Parameters

Optional Parameters

Parsed Parameters

Parameter parsing preserves types:
The type system tracks the transformation from string to number.

Search Parameter Type Safety

Search parameters are fully typed when using validation schemas.

Schema-Based Validation

Type-Safe Search Updates

Context Type Safety

Route context is fully typed through the route tree.

Defining Typed Context

Accessing Typed Context

Adding to Context

Child routes inherit and extend parent context:
The context type system is defined in packages/router-core/src/route.ts:290-293:

Loader Data Type Safety

Loader return types are automatically inferred:
The loader data type is resolved via ResolveLoaderData in packages/router-core/src/route.ts:295-299:

Type Registration

Register your router for global type inference:
Now all router hooks and components have access to your route types:
The Register interface is defined in packages/router-core/src/router.ts:115-120.

Relative Navigation Types

Relative navigation maintains type safety:
Relative paths are resolved using type-level string manipulation in packages/router-core/src/link.ts:161-200. The Link component enforces all type constraints:

Best Practices

Type registration via the Register interface gives you global type inference. Do this once in your router setup file.
Schema validation (Zod, Valibot, etc.) provides both runtime safety and compile-time types in one step.
Use params.parse to transform string params into the types your app needs (numbers, dates, etc.). The type system tracks these transformations.
Use createRootRouteWithContext to type your global context. This flows through to all child routes automatically.
Don’t manually type loader return values - let TypeScript infer them from your actual data fetching code for perfect accuracy.

Type Safety Under the Hood

TanStack Router’s type safety is powered by advanced TypeScript features:
  • Template literal types - Parse route patterns like /posts/$postId
  • Conditional types - Determine required vs optional params
  • Mapped types - Build parameter objects from paths
  • Type inference - Extract return types from loaders and context functions
  • Module augmentation - Register router globally via interface Register
The core type system lives in packages/router-core/src/route.ts and packages/router-core/src/routeInfo.ts.

Next Steps

Routes

Learn about route configuration and options

Navigation

Explore type-safe navigation APIs

Search Params

Master type-safe search parameter handling

Loaders

Understand loader data type inference