As I was going through the TS Handbook, I stumbled upon mapped types where there's a code snippet demonstrating how to wrap an object property into a proxy.
type Proxy<T> = {
get(): T;
set(value: T): void;
}
type Proxify<T> = {
[P in keyof T]: Proxy<T[P]>;
}
function proxify<T>(o: T): Proxify<T> {
// ... wrap proxies ...
}
let proxyProps = proxify(props);
While trying to enhance the proxify function with my own implementation, I encountered the following issue:
function proxify<T>(t: T): Proxify<T> {
let result = <Proxify<T>>{};
for (const k in t) {
result[k] = { //(*) from that moment I lose strong typing
get: () => t[k],
set: (value) => t[k] = value
}
}
return result;
}
The problem arises within the loop as I'm unable to enforce type control and everything defaults to any. How can I address this issue while ensuring the correctness of my implementation?