Re:Emerging JavaScript pattern: multiple return values
by Drakim on Thursday October 29, 2020 @07:31AM
(#24929357)
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.
Re:Emerging JavaScript pattern: multiple return values
by aurbano on Thursday October 29, 2020 @07:39AM
(#24929413)
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.
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.