Is there a way to use varadic arguments to recurse a type?

So like pluck({a: {b: 2}}, "a", "b") will be type safe and can somehow store the state of that type?
3 Replies
JustSomeDev
JustSomeDevOP3w ago
I know I could do something(v).pluck(x).pluck(y) but I am not a fan of that
lanc3
lanc33w ago
type RecurseKeys<O extends object, K extends keyof O> = [
K,
...(O[K] extends object ? RecurseKeys<O[K], keyof O[K]> : [])
];

const pluck = <O extends object, K extends keyof O>(
o: O,
arg_0: K,
...args: O[K] extends object ? RecurseKeys<O[K], keyof O[K]> : never
) => {
let r = o[arg_0];
for (let k in args) {
r = r[k];
}

return r;
};

const a = pluck({ a: { b: 2 }, c: { d: 3 } }, "c", "d");
type RecurseKeys<O extends object, K extends keyof O> = [
K,
...(O[K] extends object ? RecurseKeys<O[K], keyof O[K]> : [])
];

const pluck = <O extends object, K extends keyof O>(
o: O,
arg_0: K,
...args: O[K] extends object ? RecurseKeys<O[K], keyof O[K]> : never
) => {
let r = o[arg_0];
for (let k in args) {
r = r[k];
}

return r;
};

const a = pluck({ a: { b: 2 }, c: { d: 3 } }, "c", "d");
I was able to write this and this works for the arguments the return type may not be possible, at least I wasn't able to figure it out
JustSomeDev
JustSomeDevOP3w ago
oh this seems really helpful in debugging the return type, thank you!
Want results from more Discord servers?
Add your server