Created
February 8, 2019 02:57
-
-
Save uhyo/85bf237061a2321e7e9ef6347467ee38 to your computer and use it in GitHub Desktop.
Trial of typing for https://gist.github.com/hikarinotomadoi/8e61fcfa22deaafcd99c0952e52c5c28
This file contains 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
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