Created
June 14, 2019 19:22
-
-
Save ababup1192/fdf52aa39bc77fddc7ea00605b333654 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
type A = { v1: number, v2: number }; | |
type X<K1 extends keyof any, K2 extends keyof any> = { | |
[key in K1]: A | { | |
[key in K2]: A | |
}[] | |
} | |
const f = <K1 extends keyof any, K2 extends keyof any>( | |
obj: X<K1, K2> | |
): number => { | |
return Object.keys(obj).reduce((acc: number, key: string) => { | |
const item = obj[key]; | |
return (item['v1'] != undefined && item['v2'] != undefined) ? | |
acc + culcA(item as A) : | |
acc + f(item as any); | |
}, 0); | |
} | |
const culcA = (a: A): number => a.v1 + a.v2; | |
const res = | |
f({ a: { v1: 1, v2: 2 }, b: [{ c: { v1: 3, v2: 4 } }, { c: { v1: 5, v2: 6} }], d: { v1: 7, v2: 8 } }); | |
console.log(res); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment