Skip to content

A routeUrl() result with a query appended isn't a RouteUrl #893

Open

Description

@karthickeyan-dev

With routing.typedRoutes on, routeUrl() returns a branded BuiltRouteUrl since 1.10.0, but RouteUrl allows a query or hash only after an unbranded route path. Appending a query to a routeUrl() result, or to any value typed RouteUrl, gives a string that router.visit() and Link reject. Void 1.9.1 accepted the same code.

Reproduction

In a React Pages app with routing: { typedRoutes: true } and a page at /admin/users:

import { useRouter } from '@void/react';
import { routeUrl, type RouteUrl } from 'void/routes';

export function useRepro(path: RouteUrl) {
  const router = useRouter();

  void router.visit(`${routeUrl('/admin/users')}?page=2`); // TS2345
  void router.visit(`${path}?page=2`); // TS2345
  void router.visit('/admin/users?page=2'); // OK
}

Expected

A route URL with a query or hash appended is still a RouteUrl, whether it came from a route path literal or from routeUrl().

Actual

error TS2345: Argument of type '"/?page=2" | "/about-us?page=2" | ... 122 more ... | `/terms-and-conditions?${string}?page=2`' is not assignable to parameter of type 'RouteUrlInput'.
  Type '`${BuiltRouteUrl}?page=2`' is not assignable to type 'RouteUrlInput'.

The second call fails the same way, so a component that takes a RouteUrl prop can't add a query to it without a cast.

Relevant implementation

RouteUrl in the generated .void/routes.d.ts (written by the route types generator) and in void/routes' declarations is:

type RouteUrl = TypedRoutes extends true
  ? BuiltRouteUrl | RoutePathUrl | `${RoutePathUrl}${'?' | '#'}${string}`
  : string;

Adding `${BuiltRouteUrl}${'?' | '#'}${string}` to the union fixes both calls. It has trade-offs a maintainer may want to weigh, so I've left the fix to you. See Notes for a fix.

Workaround

Visit a same-page query, which Href allows:

void router.visit(search ? `?${search}` : path);

This works only when the target is the current page.

Notes for a fix

  • It also accepts unsafe joins on built URLs. BuiltRouteUrl is opaque, so the type can't tell whether routeUrl() already added a query or hash. With the extra member, both of these type-check:

    `${routeUrl('/users/[id]', { params: { id }, hash: 'details' })}?tab=activity`; // /users/42#details?tab=activity
    `${routeUrl('/a', { query: { a: 1 } })}?b=2`; // /a?a=1?b=2

    The compiler rejects these today. Route path strings already allow the same thing (the error above includes `/terms-and-conditions?${string}?page=2`), so this may be acceptable. Rejecting them would need something like a separate brand for URLs built without query or hash.

  • It only helps when the URL is passed inline. const next = `${routeUrl('/about')}?page=2` widens to string, so router.visit(next) still fails unless it uses as const.

  • RouteUrl is defined in two places. One is packages/void/src/runtime/routing/route-url.ts. The other is the lines packages/void/src/codegen/route-types.ts writes into .void/routes.d.ts. Both need the change. Applying the suffix once to a shared base means a future base type can't miss its suffixed form:

    type RouteUrlBase = BuiltRouteUrl | RoutePathUrl;
    type RouteUrl = TypedRoutes extends true
      ? RouteUrlBase | `${RouteUrlBase}${'?' | '#'}${string}`
      : string;
  • More inputs to cover in a type test. Besides router.visit(), these all accept route URLs: Link in each adapter, island forms, context.redirect() and context.rewrite(), revalidate({ paths }), and the event and live stream helpers.

Verification

Reproduced with TypeScript 7.0.2 in strict mode against the generated routes. With the extra union member added to .void/routes.d.ts, both calls type-check and router.visit('/no-such-page?page=2') is still rejected.

Environment: Void 1.11.0, @void/react 1.11.0, TypeScript 7.0.2, Node 26.13.1, macOS.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions