With TypeScript 4.5.4 (current latest), this code (see below or in TypeScript playground) is throwing error Type 'any' is not assignable to type 'never'.(2322) for the version 1,2,3,4 yet for version 5 and 6 no error is reported. If we remove one of the num prop or str, all the errors are gone.
Is there an explanation for this design?
const myObj = {
num: -1,
arr: [1, 2, 3],
str: '',
};
const proxy = new Proxy(myObj, {
set(target, prop: keyof typeof myObj, value) {
/* none of these works */
// Ver. 1
if (prop in target) target[prop] = value;
// Ver. 2
target[prop] = value;
// Ver. 3
target[prop as keyof typeof myObj] = value;
// Ver. 4
switch (prop) {
case 'num':
case 'str':
target[prop] = value;
break;
}
/* while these works */
// Ver. 5
switch (prop) {
case 'num':
target[prop] = value;
break;
case 'str':
target[prop] = value;
break;
}
// Ver. 6
switch (prop) {
case 'num':
target[prop] = value;
break;
default:
target[prop] = value;
break;
}
return true;
},
});
console.log('proxy : ', proxy);