DEV Community

Discussion on: To Typescript Or Not To?

Collapse
 
webbureaucrat profile image
webbureaucrat

I'm firmly Team TypeScript and Team Static Typing, but there's a definitional problem here I want to flag.

Just addressing the very first point as this is no different to any other strongly-typed language. When you have to deal with external sources and boundaries in any language, you don't have type safety to support you. You must always sanitise your external data. The point of TS and any strongly-typed language is that once you've validated your incoming data at runtime, you can be pretty confident that your data types in your own code are correct.

TypeScript is not strongly typed. It can't be because JavaScript is weakly typed. What TypeScript brings is static typing, which is different than strong typing, though often they are casually used interchangeably, but in the comment you're responding to the distinction is important.

I don't have time to write up full examples for both but I highly recommend looking into it.

Thread Thread
 
jackmellis profile image
Jack

Fair point. Although I think this doesn't really affect my actual point: Whether it's a statically-typed or a strong-typed language, you must sanitise incoming data at runtime, and then you can trust your static typing from then on.

This isn't something that only typescript has to deal with.

As an aside - of course, with typescript you can at any point just go "I don't care about what type this thing is" which means you lose that trust - but this is why you should avoid any or at least follow it up immediately with some form of type guards.

Thread Thread
 
latobibor profile image
András Tóth • Edited

In my own "teachings" I say as and any should only be used with external sources: either a shitty library lacking typing definitions or not knowing how to handle generics or data not yet sanitized.
When you shortcut and in fact "lie" to the type system you are setting up a bug basically.