regexparam icon indicating copy to clipboard operation
regexparam copied to clipboard

RouteParams for route with both required and multiple optional parameters gives incorrect typing

Open tec27 opened this issue 1 year ago • 1 comments

With RouteParams<'/leagues/:routeId/:slugStr?/:subPage?'> the inferred types seem to parse the route incorrectly.

Expected:

{
  routeId: string;
  slugStr?: string | undefined;
  subPage?: string | undefined;
}

Actual:

{
  "routeId/slugStr"?: string | undefined;
  subPage?: string | undefined;
}

This is TS 5.6.2 if that makes a difference

tec27 avatar Sep 11 '24 03:09 tec27

It doesn't seem like this package is being actively maintained, so if anyone has the same bug:

declare module 'regexparam' {
  type RouteParams<T extends string> =
    T extends `${infer Prev}/*/${infer Rest}`
      ? RouteParams<Prev> & { wild: string } & RouteParams<Rest>
    : T extends `${string}:${infer P}/${infer Rest}`
      ? P extends `${infer S}?`
        ? { [K in S]?: string } & RouteParams<Rest>
        : { [K in P]: string } & RouteParams<Rest>
    : T extends `${string}:${infer P}?`
      ? { [K in P]?: string }
    : T extends `${string}:${infer P}`
      ? { [K in P]: string }
    : T extends `${string}*`
      ? { '*': string }
    : T extends `${string}*?`
      ? { '*'?: string }
    : {};
...
}

Updated using an override declaration file, rather than patch-package; easy since it's only 4 types to reimplement.

finalprototype avatar Jul 30 '25 01:07 finalprototype