| ▲ | wk_end 9 hours ago | |||||||||||||
There's nothing I can do about the standard JavaScript library, but in terms of code I have influence over, I very simply would not write a difficult-to-type method like Array.prototype.flat(), if I could help it. That's what I mean by an XY Problem - why are we writing this difficult-to-type method in the first place and what can we do instead? Let's suppose Array.prototype.flat() wasn't in the standard library, which is why I'm reviewing a PR with this gnarly type in it. If I went and asked you why you needed this, I guess you'd say the answer is: "because JavaScript lets me make heterogenous arrays, which lets me freely intermix elements and arrays and arrays of arrays and... in my arrays, and I'm doing that for something tree-like but also need to get an array of each element in the structure". To which I'd say something like "stop doing that, this isn't Lisp, define an actual data type for these things". Suddenly this typing problem goes away, because the type of your "flatten" method is just "MyStructure -> [MyElements]". | ||||||||||||||
| ▲ | pcthrowaway 8 hours ago | parent | next [-] | |||||||||||||
Sure, if you're living fully in your own application code, and you don't need to consume things from an API you don't control, it's easy to live in a walled garden of type purity. I can recognize that most people are going to go for inaccurate types when fancier semantics are necessary to consume things from the network. But we also have the real world where libraries are used by both JS devs and TS devs, and if we want to offer semantics that idiomatic for JS users (such as Array.prototype.flat()) while also providing a first-class experience to TS consumers, it is often valuable to have this higher-level aptitude with the TS type system. As mentioned earlier, I believe 90% of TS devs are never in this position, or it's infrequent enough that they're not motivated to learn higher-level type mechanics. But I also disagree with the suggestion that such types should be avoided because you can always refactor your interface to provide structure that allows you to avoid them; You don't always control the shape of objects which permeate software boundaries, and when providing library-level code, the developer experience of the consumer is often prioritized, which often means providing a more flexible API that can only be properly typed with more complex types. | ||||||||||||||
| ▲ | kaoD 2 hours ago | parent | prev | next [-] | |||||||||||||
> Suddenly this typing problem goes away, because the type of your "flatten" method is just "MyStructure -> [MyElements]". How is that less maintenance burden than a simple Flatten type? Now you have to construct and likely unwrap the types as needed. And how will you ensure that you're flattening your unneeded type anyways? Sure you can remove the generics for a concrete type but that won't simplify the type. It's simple. It's just recursive flattening an array in 4 lines. Unlikely to ever change, unlike the 638255 types that you'd have to introduce and maintain for no reason. There are many reasons not to do that. Say your business logic changes and your type no longer needs one of the alternatives: you are unlikely to notice because it will typecheck even if never constructed and you will have to deal with that unused code path until you realize it's unused (if you ever do). You made code harder to maintain and more complex for some misguided sense of simplicity. | ||||||||||||||
| ▲ | 2 hours ago | parent | prev | next [-] | |||||||||||||
| [deleted] | ||||||||||||||
| ▲ | ffsm8 6 hours ago | parent | prev | next [-] | |||||||||||||
> MyStructure -> [MyElements] Right, from the structure you get an array with one element which is likely an union type from that naming. Honestly, you sound more like your arguing from the perspective of a person unwilling to learn new things, considering you couldn't even get that type correct. To begin with, that flat signature wasn't even hard to understand? | ||||||||||||||
| ||||||||||||||
| ▲ | geoffmanning 8 hours ago | parent | prev [-] | |||||||||||||
This. 1000%. | ||||||||||||||