Skip to content

Instantly share code, notes, and snippets.

@uhyo
Created February 8, 2019 02:57
Show Gist options
  • Save uhyo/85bf237061a2321e7e9ef6347467ee38 to your computer and use it in GitHub Desktop.
Save uhyo/85bf237061a2321e7e9ef6347467ee38 to your computer and use it in GitHub Desktop.
type Digged<T, Keys extends string[]> = DigFunI<T, (...args: Keys) => void>[0];
interface DigFunI<T, F> extends Array<F extends (()=>void) ? T :
F extends ((key: infer K, ...rest: infer Rest) => void) ?
K extends keyof T ? (DigFunI<T[K],((...args: Rest)=>void)> extends Array<infer E> ? E : unknown)
: unknown : unknown>{
}
function dig<T, Keys extends string[]>(obj: T, ...keys: Keys): Digged<T, Keys> {
let result: any= obj;
for (const k of keys) {
result = result[k];
}
return result;
}
const y = {
a: {
b: {
c: 10
}
}
}
let f: DigFunI<typeof y, (arg: 'a')=>void>[0];
const a = dig(y, 'a');
console.log(a.b);
const b = dig(y, 'a', 'b');
const c = dig(y, 'a','b','c');
const d = dig(y, 'a','b','xxxxxx');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment