I've been working on a personal project for the past 2 years trying to implement Scala's underscore in TypeScript, but haven't been successful. Here is my attempted implementation and its effect.
The only thing that I really care about typing is "Underscore", everything else can be ignored.
const buildUnderscore = (f: any) => (path: any[]): any => new Proxy(f, handler(path));
const handler = (path: any[]) => ({
get: function(obj: any, prop: any, value: any) {
return buildUnderscore((a: any) => getAtPath(a, path.concat(prop)))(path.concat(prop));
}
})
/* takes object A and key path ["a", "b", "c"] -> returns A["a"]["b"]["c"] */
const getAtPath = (obj: any, [head, ...tail]: string[]): any => {
if(head === undefined) return obj;
return getAtPath(obj[head], tail);
}
/* Ignore all implementation above i don't care if its typed this is the only thing i care that's typed */
const _: Underscore = buildUnderscore((a: any) => a)([]);
// ConvertKeysToFunctions<GetArgs<Array<T>["map"]>[0]> is what i need
// but there's no way to infer T
// where GetArgs gets the first argument of Array["map"]
// and ConvertKeysToFunctions converts that types keys to function types
type Underscore = <T>(a: T) => T
const data = [
{
child: {
name: "bob",
children: ["sue", "john"]
}
},
{
child: {
name: "catness",
children: ["rue", "hew"]
}
},
]
console.log(data.map(_)) // converts to data.map((a) => a)
console.log(data.map(_.child.children[0]))// converts to: data.map((a) => a["child"]["children"][0])
console.log(data.map(_.child)) // converts to data.map((a) => a["child"])