Re:Things Go needs more than generics
by cageface on Monday October 04, 2021 @05:07AM
(#28744634)
I have not found this to be the case in Typescript code I've worked on. The main issue I've found with TS is stuff coming in from outside the system as JSON that doesn't conform to expected formats.
[ Parent
| on HN ]
Re:Things Go needs more than generics
by jakelazaroff on Monday October 04, 2021 @07:02AM
(#28745200)
You can solve the JSON issue by validating that it deserializes to a known type at the point of ingress. I wrote a small library that I use in basically every project to help with that: https://www.npmjs.com/package/narrows
One issue I've noticed is that indexing into a T[] array where T can't be undefined, results in the expression to be of type T, not T | undefined, despite an out of bounds index evaluating to undefined. I believe I've seen that they recently added a setting that changes this though.
A lot of statically typed languages leave array indexing as a loophole for convenience but yeah this can also be a source of errors.
Consider using https://github.com/gcanti/io-ts to help with that...