kenchris / urlpattern-polyfill

URLPattern polyfill
https://www.npmjs.com/package/urlpattern-polyfill
MIT License
267 stars 30 forks source link

Please consider adding more complex typing for result groups #130

Open ghost opened 2 months ago

ghost commented 2 months ago

Right now the typing for URL pattern component result groups is basically just Record<string, string | undefined>.

However, take a look at this: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/7d182347b9be493cd37ad26a0b7a8285310cc914/types/express-serve-static-core/index.d.ts#L108

You can see it is able to infer the correct params based on a string.

For example:

const pathname = "/about/:name/:age?";

const props = async (groups: RouteParameters<typeof pathname>) => {
  return { name: groups.name };
};

It infers the types perfectly, and works with optional params, and infinite params:

(parameter) groups: {
    name: string;
} & {
    age?: string | undefined;
}

For now I have added this package to my project, but it would be nice if this lib implemented the same typing for groups.