Last active
February 25, 2022 23:26
-
-
Save ebeloded/543e37cf89e1199c1a32528ba07ca8b0 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
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