Skip to content

Instantly share code, notes, and snippets.

@asvny
Forked from natemoo-re/RouteParams.ts
Created November 4, 2020 19:30
Show Gist options
  • Select an option

  • Save asvny/e2bb0f14bc8bcb0ef581fbd2e73862e3 to your computer and use it in GitHub Desktop.

Select an option

Save asvny/e2bb0f14bc8bcb0ef581fbd2e73862e3 to your computer and use it in GitHub Desktop.

Revisions

  1. @natemoo-re natemoo-re revised this gist Nov 3, 2020. 1 changed file with 9 additions and 8 deletions.
    17 changes: 9 additions & 8 deletions RouteParams.ts
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,16 @@
    type FilterParams<S extends string> = S extends `[${infer A}]` ? A : never;

    type NormalizePath<S extends string> = S extends `/${infer T}` ? T : S;
    type RestParam<S extends string> = S extends `...${infer A}` ? A : never;
    type StandardParam<S extends string> = S extends `...${infer A}` ? never : S;

    type ExtractParams<S extends string> = S extends `[${infer A}]` ? A : never;
    type TupleToUnion<T extends any[]> = T[number];
    type Split<S extends string> =
    string extends S ? string[] :
    S extends '' ? [] :
    S extends `${infer T}/${infer U}` ? [T, ...Split<U>] :
    [S];
    type NormalizePath<S extends string> = S extends `/${infer T}` ? T : S;

    type TupleToUnion<T extends any[]> = T[number];

    export type RouteParams<S extends string> = { [param in FilterParams<TupleToUnion<Split<NormalizePath<S>>>>]: string | number };

    const test: RouteParams<'/blog/[id]'> = { id: '1' };
    type AllPathParams<S extends string, P extends string = ExtractParams<TupleToUnion<Split<NormalizePath<S>>>>> = { [param in P]: string|string[] };
    type RestParams<S extends string, Base extends string = keyof AllPathParams<S>> = { [a in RestParam<Base>]: string[] };
    type StandardParams<S extends string, Base extends string = keyof AllPathParams<S>> = { [a in StandardParam<Base>]: string };
    export type PathParams<S extends string> = RestParams<S> & StandardParams<S>;
  2. @natemoo-re natemoo-re renamed this gist Nov 3, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions PathParams.ts → RouteParams.ts
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,6 @@ type Split<S extends string> =

    type TupleToUnion<T extends any[]> = T[number];

    export type PathParams<S extends string> = { [param in FilterParams<TupleToUnion<Split<NormalizePath<S>>>>]: string | number };
    export type RouteParams<S extends string> = { [param in FilterParams<TupleToUnion<Split<NormalizePath<S>>>>]: string | number };

    const test: PathParams<'/blog/[id]'> = { id: '1' };
    const test: RouteParams<'/blog/[id]'> = { id: '1' };
  3. @natemoo-re natemoo-re created this gist Nov 3, 2020.
    15 changes: 15 additions & 0 deletions PathParams.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    type FilterParams<S extends string> = S extends `[${infer A}]` ? A : never;

    type NormalizePath<S extends string> = S extends `/${infer T}` ? T : S;

    type Split<S extends string> =
    string extends S ? string[] :
    S extends '' ? [] :
    S extends `${infer T}/${infer U}` ? [T, ...Split<U>] :
    [S];

    type TupleToUnion<T extends any[]> = T[number];

    export type PathParams<S extends string> = { [param in FilterParams<TupleToUnion<Split<NormalizePath<S>>>>]: string | number };

    const test: PathParams<'/blog/[id]'> = { id: '1' };