What you consider as an array is actually meant to be a tuple. JavaScript doesn't have an extra data type for them, but its heterogeneous arrays work well. As you said, it's a pair of results, not an array.
Why would one prefer to return a pair of results as an array, instead of returning an object?
Because of its simplicity. You don't need to declare some verbose type (assuming TypeScript) and come up with names for the properties. It's not a StateUse<S> { state: S, setState: S => S }
result, it's a simple Tuple<S, S=>S>
or [S, S=>S]
(depending on what syntax you prefer).
It's exactly the same reasoning why we use functions with multiple parameters instead of a parameter object when there are only a few parameters, just applied the output type instead of the input type.
It seems like the benefit of object destructuring is that it's named, more familiar, and order independent.
Array destructuring is is even simpler than object destructuring, and should be familiar as well. When dealing with only two result values and never changing that, a tuple is just simpler. It is expected that one would never need to add another property (in that case the object would be superior).
Not having property names makes it easier (less verbose) to use aliases as the destructuring targets. It forces the user to come up with a good, appropriately descriptive variable name himself.
That it relies on an order doesn't matter, the order just follows conventions like [getter, setter]
or [key, value]
, and you typically always want to use both values so there's no benefit in being able to omit a property from destructuring.