Skip to content

Route parameter type provides wrong type for named routes #2169

@jangxyz

Description

@jangxyz

Having

<Route path="/product/{id:Int}" page={ProductPage} name="product" />

would make routes.product({ id }) method available, but only string values are available.
This raises false error on .ts files.

routes.product({ id: 123 }) // Type 'number' is not assignable to type 'string'. ts(2322)

image

Here is the generated .redwood/types/routes.d.ts file:

import '@redwoodjs/router'

type ParseRouteParameters<Route> =
  Route extends `${string}/{${infer Param}:${string}}/${infer Rest}`
    ? { [Entry in Param | keyof ParseRouteParameters<`/${Rest}`>]: string }
    : Route extends `${string}/{${infer Param}:${string}}`
      ? { [Entry in Param]: string } // <-- HERE?
      : Route extends `${string}/{${infer Param}}`
        ? { [Entry in Param]: string }
        : Record<string, string>

declare module '@redwoodjs/router' {
  interface AvailableRoutes {
    product: (params?: ParseRouteParameters<"/product/{id:Int}">) => "/product/{id:Int}"
  }
}

It should at least support core route parameter types.

I tried adding these two lines, and it seems to fix the issue.

type ParseRouteParameters<Route> =
  Route extends `${string}/{${infer Param}:${string}}/${infer Rest}`
    ? { [Entry in Param | keyof ParseRouteParameters<`/${Rest}`>]: string }

    // check for Int type
    : Route extends `${string}/{${infer Param}:Int}`
      ? { [Entry in Param]: number }

      : Route extends `${string}/{${infer Param}:${string}}`
        ? { [Entry in Param]: string }
        : Route extends `${string}/{${infer Param}}`
          ? { [Entry in Param]: string }
          : Record<string, string>

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions