JavaScript/typescript is very dynamic. Which makes it very cool to do things like these - but I'm struggling to see where this will be beneficial? Having a class/interface with those properties on it is much easier to work with, pushing this type into a normal array. It also improves readability. Just because you can doesn't mean you should. I don't know - it's just my personal opinion. If you spend too much time trying to figure out what the previous developer did, you're already failing in writing maintainable code.
I partially agree with you @jaspeling
. We can return a class/interface. But at the same time it is type introduced in Typescript. And it is very common while writing custom hooks to return an array as I explained in my example. In React useState uses same kind of pattern.
JavaScript/typescript is very dynamic. Which makes it very cool to do things like these - but I'm struggling to see where this will be beneficial? Having a class/interface with those properties on it is much easier to work with, pushing this type into a normal array. It also improves readability. Just because you can doesn't mean you should. I don't know - it's just my personal opinion. If you spend too much time trying to figure out what the previous developer did, you're already failing in writing maintainable code.
Thanks for your valuable insight.
I partially agree with you @jaspeling . We can return a class/interface. But at the same time it is type introduced in Typescript. And it is very common while writing custom hooks to return an array as I explained in my example. In React
useState
uses same kind of pattern.Ah, see I have no react knowledge. If that is a pattern used in a very well known framework, I guess I can see the value in a post like this.
I see. Yes, it can be bit confusing at first. When I switched to react and saw this pattern I had the same feeling 😊