So instead of returning a object, we stuff two values into an array and this grants us the advantage of saving a few extra characters. This is pretty hacky and it smells. it also won't work in languages like typescript (without losing the advantage of using it) which is frequently used.
Did we read the same article? I do absolutely not see the author advocating returning values in an array instead of an object, in fact, they seems to be saying the exact opposite? (starting with the array example, and then going to the object example afterwards) The primary point of the article is the destructuring which makes the syntax much nicer.
Emerging JavaScript pattern: multiple return values
21–24 of 24 posts
Re: Emerging JavaScript pattern: multiple return values
#22So instead of returning a object, we stuff two values into an array and this grants us the advantage of saving a few extra characters. This is pretty hacky and it smells. it also won't work in languages like typescript (without losing the advantage of using it) which is frequently used.
While the article does advocate for objects vs arrays for returning multiple values I wanted to address the second part of you comment. It will work just fine in TypeScript, where you can define a "fixed size" array as a type: type Test = [number, string]; const test: Test = [1, 'something']; The second line will fail compilation if there's a different number of elements in that array, or if the types don't match.
As for the custom type in typescript. I am not too keen on that approach but I suppose it is valid.