-
-
Save asvny/e2bb0f14bc8bcb0ef581fbd2e73862e3 to your computer and use it in GitHub Desktop.
Revisions
-
natemoo-re revised this gist
Nov 3, 2020 . 1 changed file with 9 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,15 +1,16 @@ 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 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>; -
natemoo-re renamed this gist
Nov 3, 2020 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 RouteParams<S extends string> = { [param in FilterParams<TupleToUnion<Split<NormalizePath<S>>>>]: string | number }; const test: RouteParams<'/blog/[id]'> = { id: '1' }; -
natemoo-re created this gist
Nov 3, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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' };