Skip to content

Instantly share code, notes, and snippets.

@AntsiferovMaxim
Last active April 9, 2020 22:53
Show Gist options
  • Save AntsiferovMaxim/c0cb96ff4f0e68a634118a892ee282ef to your computer and use it in GitHub Desktop.
Save AntsiferovMaxim/c0cb96ff4f0e68a634118a892ee282ef to your computer and use it in GitHub Desktop.
type RouteConfig<C extends RouteChildren = {}> = {
[P in keyof C]: RouteConfig;
} & {
(): string;
children: RouteChildren;
};
type RouteChildren = {
[key: string]: RouteConfig;
};
export function route<C extends RouteChildren>(path: string, children: RouteChildren = {}) {
const fn: RouteConfig = function() {
return path
}
fn.toString = () => path;
fn.children = children;
Object.entries(children).forEach(([key, nested]) => {
// @ts-ignore
fn[key] = route(path + nested(), nested.children);
})
return fn
}
const routes = {
root: route('/'),
home: route('/home', {
user: route('/user', {
notifications: route('/notifications'),
settings: route('/settings'),
}),
dashboard: route('/dashboard'),
}),
}
console.log(routes.home.user.settings())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment