Last active
April 9, 2020 22:53
-
-
Save AntsiferovMaxim/c0cb96ff4f0e68a634118a892ee282ef to your computer and use it in GitHub Desktop.
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 characters
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