Experimenting with Proxies has been a challenge for me as I struggle to get even the most basic form up and running.
Below is the code snippet:
const myObj = {
x: 'Hello',
};
const p = new Proxy(myObj, {
get: (target, key) => {
return key in target ? target[key] + ' World!' : 'nope';
},
});
console.log(p.x);
I encountered an error which seems confusing, leaving me unsure of how to resolve it:
index.ts:7:28 - error TS7053: Element implicitly has an 'any' type because expression of type 'string | number | symbol' can't be used to index type '{ x: string; }'.
No index signature with a parameter of type 'string' was found on type '{ x: string; }'.
7 return key in target ? target[key] + ' World!' : 'nope';
~~~~~~~~~~~
Found 1 error.
My understanding is that TypeScript should be able to infer everything here. What am I missing?