I'm using TypeScript along with TSLint, and I have the following code:
var myObj = {}
var id = "key"
myObj[id] = 1
delete myObj[id]
But I receive a hint from TSLint: Do not delete dynamically computed property keys. (no-dynamic-delete)
The rationale for this rule (as stated on the documentation for TSLint):
Deleting dynamically computed keys is dangerous and not well optimized.
My question is, without disabling this hint in the TSLint configuration file, how should I safely and optimally delete the id
key in myObj
?
delete myObj.key
without the[]
? – Dramatistdelete myObj[id]
is not the same asdekete myObj.id
. – ThoritemyObj[id]
uses the variableid
, not the field in the object with the key ofid
. For examplelet id = 'myKey'; myObj[id]
this usesmyObj.myKey
but with a variable. ButmyObj.id
doesn't use any variables for the key, only theid
field directly. – Thorite