Last active
January 4, 2022 09:55
-
-
Save PabloLION/9606bbdf435476bcc680bbacce894e6b to your computer and use it in GitHub Desktop.
Writing produces type intersection in TS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/microsoft/TypeScript/issues/47295 | |
// https://stackoverflow.com/questions/70569105/proxy-property-in-setter-has-never-type | |
// https://www.typescriptlang.org/play?ts=4.6.0-dev.20220103#code/MYewdgzgLgBAtgTwPICMBWMC8MDeBYAKBhjAFc4AuGAWgEYAaQ4gQwCdWqBtBmAJnpgBmALqMiMaBxgByaWIC+AbkKFQkWAAdWIAB4IsJAKYB3GAAVtegBSJUaAfnERDUK1DYBzFwK0gNVAGtDBBAAMxgoBA1DMPhkdAEAN2YAG1JDAEpcJmIYAHoAKhJwQxhYqAALQ2cYYxBWAIgYAryc4jy8mAA1Q1YAOhhaNpgAS3CrXw1RsAjPFyz3Vi8oTknhA2S0w2Vxds6e-r5hxeXV7Q117E30ndz8-d6BwWO5lcmYZiagkPDI6NjbOhLjBrtthh1uo8YAAWYYQYwjKDACowCbnLKOO7EYCfUrSMhwaQUYbY3EySREkm5E4uM5+YGg25Y4goViGZgBJnEeQqXb5IrGCojFKlSrVUp1BpNFrgh6HACscIRSJRaL8GKpOJq+PIlL5dxpb3ODNSNypLLZHK5uS1eIpxP11NedIuG1NYMdFvZnOGPL5EIOAwAbErEcjUZMNY7bTICXrmcRDS6TVtrXdWd60zAACaGULMUgpKAOhOzJa0tZu1PmmAZq2+3l3NlQUisGZQVhm8TyBQZHaqcAQEAivopEAeKzSXx6GBUOQwacIPuEIA | |
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment