Hacker News new | ask | show | jobs
by LiterallyDoge 2758 days ago
What did you find you liked about Typescript? I considered it and opted for pure ES6/Next because the typing didn't feel like it would actually help sanitize my data. If I get a bad payload from the server, it's still transpiled at that point, and that's when I really need to know what my data type is. Otherwise I just sanitize at use (like all JS) and the overall overhead is less. Maybe you know something that I don't?
3 comments

It's pretty useful for libraries, as you don't have to do anything, but still get type safety benefits. It also has really good tooling. If you use anything like ES Lint, then TypeScript does a similar job but is a step up in quality.
In my experience, retrieving external data is not the point at which Typescript is useful – it's usually safe to the make the assumption that whatever data you retrieve meets the contract you've already agreed. But it does prevent whole classes of bugs that I found were pretty common in plain JS:

- "This thing was null but you didn't account for this case" - "You should have a number here but something returned a string" - "You have made an invisible typo in a property name" - "You have not handled all possible values in this function" - "You accessed some data internal to a class that wasn't meant to be changed"

Basically my experience is that is substantially cuts down on the number of stupid bugs that end up in my code. The code completion is also fantastic!

Maybe you will be interested in a library I wrote:

https://github.com/Dean177/jest-to-match-shape-of

I had the same issue, types don't mean anything if they don't align with the data that your app is consuming. My solution was that matcher combined with some integration tests.