Closed d0whc3r closed 11 months ago
Hey @d0whc3r, thanks for submitting the PR.
I tested it out and found an issue — if there's a splat with another param it causes type keys duplication:
export type Path =
| `/`
| `/about`
| `/posts`
| `/posts/:id`
| `/posts/:id/:pid?`
| `/posts/:id/deep/*` // ←
export type Params = {
'/posts/:id': { id: string }
'/posts/:id/:pid?': { id: string; pid?: string }
'/posts/:id/deep': { id: string } // ←
'/posts/:id/deep': { '*': string } // ←
}
Mostly we'd need to add both params and splats to the same key in the previous condition:
if (param.length) {
params.push(`'/${path}': { ${param.map((p) => p.replace(/:(.+)(\?)?/, '$1$2:') + ' string').join('; ')} }`)
}
Please let me know if you'd like to give it a try.
Thanks!
@oedotme fixed 😄
Hey @d0whc3r, I've pushed a small change to improve readability and to remove unnecessary splat condition. In addition, the Path
type needed to be updated as well — as each Params
key should match a Path
.
Thanks again, I'll be releasing this shortly.
Following the example,
router.ts
file will be like this:adding
'/splat/*': { '*': string }
because "useParams" in a splat file will return an object with one keyhttp://.../splat/asdf/a/d/b/e