Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

33345 - Dynamic Route #33614

Open
lvjiaxuan opened this issue Jun 26, 2024 · 2 comments
Open

33345 - Dynamic Route #33614

lvjiaxuan opened this issue Jun 26, 2024 · 2 comments
Labels
33345 answer Share answers/solutions to a question en in English

Comments

@lvjiaxuan
Copy link
Contributor

type IsArr<T> = T extends `${string}...${string}` ? true : false
type Extract<T> = T extends `[...${infer F}]` | `...${infer F}` ? F : T

type DynamicRoute<T extends string, _Res extends Record<string, string | string[]> = {}> = T extends `${infer A}[...]${infer B}`
  ? DynamicRoute<`${A}${B}`, _Res & { '...': string }>
  : T extends `${string}]/[...${string}`
    ? never
      : T extends `${infer A}[[${ infer F }]]${ infer B }`
      ? DynamicRoute<`${A}${B}`, _Res & ( Extract<F> extends '' ? {} : { [K in Extract<F>]?: IsArr<F> extends true ? string[] : string } ) >
      : T extends `${infer A}[${ infer F }]${ infer B }`
        ? DynamicRoute<`${A}${B}`, _Res & ( Extract<F> extends '' ? {} : { [K in Extract<F>]: IsArr<F> extends true ? string[] : string } ) >
    : { [K in keyof _Res]: _Res[K] }
@lvjiaxuan lvjiaxuan added answer Share answers/solutions to a question en in English labels Jun 26, 2024
@teamchong
Copy link

It's good, but I believe adding this test case Expect<Equal<DynamicRoute<'/[[...foo]]/[...]/[...bar]'>, never>> will result in a failure, even though it is supposed to pass.

@lvjiaxuan
Copy link
Contributor Author

It's good, but I believe adding this test case Expect<Equal<DynamicRoute<'/[[...foo]]/[...]/[...bar]'>, never>> will result in a failure, even though it is supposed to pass.

Thanks for testing. This is my fix.

type IsArr<T> = T extends `${string}...${string}` ? true : false
type Extract<T> = T extends `[...${infer F}]` | `...${infer F}` ? F : T

- type DynamicRoute<T extends string, _Res extends Record<string, string | string[]> = {}> = T extends `${infer A}[...]${infer B}`
+ type DynamicRoute<T extends string, _Res extends Record<string, string | string[]> = {}> = T extends `${infer A}/[...]${infer B}`
  ? DynamicRoute<`${A}${B}`, _Res & { '...': string }>
  : T extends `${string}]/[...${string}`
    ? never
      : T extends `${infer A}[[${ infer F }]]${ infer B }`
      ? DynamicRoute<`${A}${B}`, _Res & ( Extract<F> extends '' ? {} : { [K in Extract<F>]?: IsArr<F> extends true ? string[] : string } ) >
      : T extends `${infer A}[${ infer F }]${ infer B }`
        ? DynamicRoute<`${A}${B}`, _Res & ( Extract<F> extends '' ? {} : { [K in Extract<F>]: IsArr<F> extends true ? string[] : string } ) >
    : { [K in keyof _Res]: _Res[K] }

play

@lvjiaxuan lvjiaxuan reopened this Jun 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
33345 answer Share answers/solutions to a question en in English
Projects
None yet
Development

No branches or pull requests

2 participants