When attempting to utilize a Proxy for adding logic whenever a value in my array is set to a new value, I encountered an issue with TypeScript. The error arises from the fact that TypeScript does not support indexing an array with anything other than a number.
const nums = [1, 2, 3, 4, 5];
const handler: ProxyHandler<number[]> = {
set(target: number[], property: string | symbol, newValue: any) {
target[property] = newValue;
return true;
},
};
const proxy = new Proxy(nums, handler);
Specifically, I am receiving the error
Element implicitly has an 'any' type because index expression is not of type 'number'.ts(7015)
when trying to assign a new value with target[property] = newValue;
.
I am now seeking advice on how to resolve this issue and make TypeScript happy. Any suggestions or solutions would be greatly appreciated.