Skip to content

TypeScript Support

Written in strict TypeScript with no any in the public API surface.

Param Type Inference

Param types for .build() are inferred directly from each path template via a recursive template-literal type:

ts
PATHS.USERS.EDIT.build({ id: 42 });     // ✓ compiles
PATHS.USERS.EDIT.build({});             // ✗ compile error — 'id' is required
PATHS.USERS.EDIT.build({ userId: 42 }); // ✗ compile error — 'id' expected, not 'userId'

Typed paramNames

.paramNames is typed as a literal array of the exact param names in the template, not a generic string[]:

ts
PATHS.USERS.EDIT.paramNames; // typed as ['id'], not string[]

Type Exports

The package exports the following types for advanced use cases:

TypeDescription
RoutePathA static route string, e.g. '/users/:id'
RouteBuilderA route builder function
RouteParamAccepted param value types (string | number | boolean)
RouteParamsRecord of param name to value
QueryParamsQuery parameter record with array/null/undefined support
RouteLeafEither a static path or a builder function
RouteMapRecursively defined route map
RouteTreeAnnotatable shape of a route map for defineRoutes
ExtractParamsExtracts param names from a template string type (incl. '*' for splats)
PathParamsBuilds a params object type from a path template
BuildPathOptionsOptions for buildPath (strict, encode, hash)
FlatRouteA single flattened route entry ({ key, path })
BreadcrumbItemA single breadcrumb entry
BreadcrumbOptionsOptions for getBreadcrumbs (resolver or labels map)
StaticRouteA static route leaf — string-coercible with .build(query?, options?)
DynamicRouteA dynamic/splat route leaf — with .build(params, ...) and .paramNames
MatchPathOptionsOptions for matchPath (end, caseSensitive)

Annotate a plain route object with RouteTree before passing it to defineRoutes():

ts
import type { RouteTree } from "react-routes-forge";

const routes: RouteTree = {
  HOME: "/",
  USERS: { ROOT: "/users", EDIT: "/users/edit/:id" },
};

Requirements

  • TypeScript ≥ 5 recommended for full type inference
  • The package works with plain JavaScript too, just without compile-time param checking

Released under the MIT License.