Why Typescript Map is not automatically indexable (& property-accessable) like Indexable Types? -


so, in typescript below code works:

interface counterarray {     [index: string]: number; }  let myarray: counterarray = {}; myarray["001"] = 0  myarray["002"] = 0  console.log(myarray[0]) // should result 0 

got it. good! but, if map.

const map = new map<string, number> map.set("001",0) map.set("002",0)  console.log(map[0]) // syntax error: element implicitly has 'any' type because type 'map<number,string>' has no index signature. 

well i'm quite baffled this, because, coming java/kotlin/c#, map default should index-able key. , think, such work should done elegantly using map<key,value>.

moreover, using "first code block above", can like:

console.log(myarray["001"]++) // should results 1 

while can't map counterpart

console.log(myarray.get("001")++) // syntax error, object possibly undefined -> on editor level. it's syntax error. not compile or other runtime error. 

i think behavior weird. or maybe expected? maybe map know (from java/c#/kotlin) different typescript's map?

thanks explanation!


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -