Hacker News new | ask | show | jobs
by edvald 2940 days ago
I'm a pretty heavy TS user and I was pondering the same thing. I've not been able to find a motive for allowing numbers as interface keys, but Symbols actually do make sense since, turns out (I'm always learning something new), you can use them as object keys in ES6: https://www.keithcirkel.co.uk/metaprogramming-in-es6-symbols...

That post outlines a few use cases which are admittedly outside of everyday JS/TS usage but I can see them as quite powerful when, for example, building frameworks and libraries that expose an API. I'd offer real-world examples of my own but I'm still working it out for myself .)

1 comments

JS allows numbers as object keys (though it's one of those weird behavioral swamps where arrays act differently from objects, and sometimes they are coerced to strings [objects] and sometimes not [arrays], depending), so this mostly seems that its just about getting better at defining the existing JS behaviors of things, modulo the "quirks" of course.

A lot of this matters in "metaprogramming" like Readonly<T> and Partial<T> which use all of the existing keys in a type to produce a "new" result (none of the key can be set, and all of the keys are optional, respectively), and making sure it doesn't miss intentional keys like symbols and numbers.