I find the code below quite strange...
export class Collection {
private data: {[k: string]: any} = {};
constructor () {
// This part works fine
this.data["hello"] = "hello";
// Unexpectedly works
this.data[2] = 2;
}
}
export class Collection2 {
private data: {[k: symbol]: any} = {};
constructor () {
// Doesn't work as expected
this.data["hello"] = "hello";
// Unexpectedly doesn't work
this.data[Symbol.iterator] = function () {}
}
}
Setting the index signature to string
should only allow strings to index it, right? Similarly, with symbols. However, numbers can index [k: string]
and attempting to use symbol
as an index signature results in an error.