Skip to content

Instantly share code, notes, and snippets.

@Peeja
Created February 23, 2021 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Peeja/40b043eb02287fa3e63679427cac96b6 to your computer and use it in GitHub Desktop.
Save Peeja/40b043eb02287fa3e63679427cac96b6 to your computer and use it in GitHub Desktop.
type PathSegments<Path extends string, Segments = never> = Path extends `${infer Segment}/${infer Rest}` ? PathSegments<Rest, Segments | Segment> : Segments | Path;
type ParamSegments<S extends string> = S extends `:${infer P}` ? P : never;
let segments: PathSegments<'/posts/:id/comments/:comment_id'>;
interface Routes {
match<Path extends string>(path: Path, cb: (params: Record<ParamSegments<PathSegments<Path>>, string>) => void): void;
}
declare const routes: Routes
routes.match('/posts/:id/comments/:comment_id', (params) => {
console.log({params})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment