Exploring various TypeScript idioms showcased in the responses to this Stack Overflow post (Iterating over Typescript Map) on Codepen.
Below is my code snippet.
class KeyType {
style: number;
constructor(style) {
this.style = style;
};
}
function test() {
const someMap = new Map<KeyType, number>();
someMap.set(new KeyType(style=199), 5);
someMap.set(new KeyType(style=67), 90);
console.log('testing 1')
for (let [k, m] of someMap) {
console.log(`1 k ${k} m ${m}`);
}
console.log('testing 2')
for (let [k, m] of someMap.entries()) {
console.log(`2 k ${k} m ${m}`);
}
console.log('testing Object entries')
for (let [k, m] of Object.entries(someMap)) {
console.log(`3 k ${k} m ${m}`);
}
console.log('forEach method')
someMap.forEach((v, id) => console.log(`3 v ${v} id ${id}`))
Array.from(someMap.entries()).forEach(entry =>
console.log(`4 k ${entry[0].style} m ${entry[1]}`)
)
const ar = Array.from(someMap.keys());
console.log(ar[0].style);
}
My confusion arises from the fact that all the forEach
method functions as expected, while the for (let [k,m] of someMap) {...
does not seem to work at all.
Could it possibly be an issue with CodePen's TypeScript setup? Or have I made an error within the testing 1
, testing 2
, and testing Object entries
sections above?
I intended to try installing ts-node
for local testing but encountered different problems during its installation process.