6 ms·
>> It's impossible in JavaScript. It's definitely possible (just as easy in fact). You just need to define a schema for each API endpoint. TypeScript does not
by cryptica 7y ago
>> It's impossible in JavaScript.
It's definitely possible (just as easy in fact). You just need to define a schema for each API endpoint. TypeScript does not physically protect you from having to enumerate all the properties and size constraints of the data one by one. In terms of the actual schema validation step, there are lots of tools in JavaScript which let you do the same thing; ajv, z-schema are just a couple of examples.
My point is that TS adds no value there. TS's value is only in static type checking. What the article is claiming is that TS somehow adds value with runtime type checking.
The argument of saying that "TypeScript adds value because it can reuse its internal type definitions for the purpose of validating remote input" is circular - It refers to a problem which doesn't exist in JavaScript to begin with.
JavaScript has no type definitions so being unable to reuse type definitions for schema validation does not qualify as a drawback on its part. The argument simply does not apply and cannot be used to claim TS's superiority.
At best you could claim that TypeScript cancels out one of its own shortcomings:
- TS shortcoming: You need to define types everywhere... That adds a lot of work! (-1 point)
- TS benefit: But you can re-use these type definitions for doing schema validation of user input as well! That saves a lot of work! (+1 point)
But the net gain over JS is 0 because:
- JS shortcoming: You need to define a schema for all your endpoints to validate user input... That adds a lot of work! (-1 point)
- JS benefit: But aside from that, you don't need to define types anywhere... So that saves you a lot of work. (+1 point)
The only real argument to be had is whether or not compile-time static typing adds value over dynamic typing.
- gary_bernhardt 7y agoThe post isn't about dynamic validation; it's about static type checking of APIs, which is impossible in JavaScript.
- cryptica 7y agoMy point is that static type checking of APIs does not solve any new problem which static type checking in general does not already solve - That's why I don't agree that it solves API woes specifically. My argument is that it adds no value in that area. If a developer already does input validation correctly with JavaScript, then switching to TypeScript will not add any value for them in that area. Furthermore, my first argument (about TS giving false confidence) is that TypeScript does not make it any more likely that an unskilled developer would be able to identify and solve the problem of schema validation when compared to JavaScript... From the unskilled developer's point of view, false confidence (which TypeScript can sometimes provide) is worse than having no confidence at all (which is what JavaScript always provides). Type checking is 100% about confidence; it gives more confidence. Confidence and correctness are two completely different things and I wanted to point out that there is such a thing as false confidence.
- gary_bernhardt 7y agoI rename the "email" key in our register endpoint to "emailAddress" and all code that touches that key turns red less than 1s later, whether it's in the client or server. Edit: All of these edits to your posts after I've already replied are very confusing.
- cryptica 7y agoWhat you're describing applies to static type checking in general. I don't argue this point. Sorry about the edits. It's very difficult to express such things unambiguously. I'm more concerned about the effects of the post's title on people's programming practices than the actual content of it. HN does tend to have this effect unfortunately.
- cryptica 7y agoAlso, I should point out that in terms of achieving correctness, it's possible to get similar value as what you're describing simply by having good tests. I don't want to get into this argument now though because it could last forever but hopefully it highlights why it's important to keep arguments tightly bounded and not make blanket statements when the domain is so large and complex and we can go off on a tangent in an infinite number of directions. Ultimately, I enjoy JavaScript and I don't want future employers to force me to use TypeScript because of articles like yours (with those kinds of titles)... I was already forced to use TypeScript at my last company, it went well but it would have been even better if founders had let me and my team use JavaScript... For one, I might not have quit the company. I was tired of debugging mangled JavaScript (compiled from TS) using vim over SSH whenever there was a problem (it was a large decentralized P2P project so often that was the only way to debug it). The drawbacks of TS were definitely not worth its benefits for that specific project.
- gary_bernhardt 7y agoYou may enjoy this talk: https://www.destroyallsoftware.com/talks/ideology https://www.destroyallsoftware.com/talks/ideology
- dsun176 7y agoWe created a proxy that only tunnels data that matches the swagger.json. https://github.com/EXXETA/openapi-cop https://github.com/EXXETA/openapi-cop This is cool for dev mode but you do not always want it in production because deep validation of all requests and responses takes many cpu cycles.