I've written an update function that takes an object, T, and an "updater" object, which takes the same keys (or a subset) as T and provides a function for updating the corresponding value of the original object.
type UpdaterObj<T> = {
[K in keyof Partial<T>]: (t: T[K]) => T[K]
};
declare function update<T>(obj: T, updaterObj: UpdaterObj<T>): T
Sample usage is like this:
const foo = {
x: 1,
y: "1"
};
update(foo, {
x: (xx: number) => xx + xx,
y: (yy: string) => yy + yy,
}); // Result is { x: 2, y: "11" }
This works, but I'd really like to remove those annotations and just write x: (xx) => xx + xx and y: (yy) => yy + yy, however I get the Parameter implicitly has type 'any' error, for both xx and yy.
It's odd because if I mouse over x:, it correctly infers what the corresponding value should but, and it complains if I annotate xx or yy as the wrong type, and I'm pretty sure I've seen typescript make similar inferences before.
The annotations are pretty simple in this case, but can get annoying if I'm deep within objects or with more complex types, so inference would be really helpful here.
Is there any simple tweak to my definitions or my usage that can get typescript to infer this for me?