4 ms·
Maybe it's too early in the morning but i had a really hard time understanding the changes around keyof and mapped types and what it's good for. It felt very ma
by Too 8y ago
Maybe it's too early in the morning but i had a really hard time understanding the changes around keyof and mapped types and what it's good for. It felt very mathematically academic. Does anyone have real world examples where it's useful?
- edvald 8y agoI'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/ 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 .)
- WorldMaker 8y agoJS 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.