Skip to content

Instantly share code, notes, and snippets.

@ebeloded
Last active February 25, 2022 23:26
Show Gist options
  • Save ebeloded/543e37cf89e1199c1a32528ba07ca8b0 to your computer and use it in GitHub Desktop.
Save ebeloded/543e37cf89e1199c1a32528ba07ca8b0 to your computer and use it in GitHub Desktop.
const objectify = <T, EXT extends Object>(
f: (v: string) => T,
ext?: EXT,
root?: string
): Record<string, T> & EXT =>
new Proxy(f, {
get: (target, prop) =>
typeof prop === 'string'
? ext?.[prop] || target(prop as string)
: () => root,
}) as any
const x = objectify(
(v) => ({
a: [v, 'bla'].join('/'),
}),
{
b: 'test',
},
'x'
)
x['asdf'].a /*?*/
x.b /*?*/
x /*?*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment