3 ms·
Typescript only has compile time checking. There are so many sharp blades that allow that to be bypassed. For example, deserialize something into a variable y
by tomohawk 4y ago
Typescript only has compile time checking. There are so many sharp blades that allow that to be bypassed. For example, deserialize something into a variable you've declared to be a certain type, and viola. The typing is incredibly weak.
- tadfisher 4y agoMost static languages only have compile-time type checking, because storing and checking runtime type information is usually not worth the cost. The problem with your example is the deserializer, which in a static language would be generated and include those runtime checks. I'm sure something like this exists for Typescript, it's too common a problem not to have been solved.
- tomohawk 4y agoIn Go, can I declare a Foo, and deserialize into it, but instead of a Foo, it is a Bar? No. Typescript also doesn't catch cases where metaprograming is used when calling a function. You can put anything you want in there.
- bluefirebrand 4y ago> For example, deserialize something into a variable you've declared to be a certain type, and viola. I'm pretty sure your linter will catch this. If you don't have a linter, get a linter.
- infinitebit 4y agothis has been solved by several packages, runtypes https://github.com/pelotom/runtypes https://github.com/pelotom/runtypes being my favorite
- tomohawk 4y agoThanks for the pointer to this, but this also makes Crockford's point about the need for a language that does this sort of thing and my point that in order to achieve safety, you need to know about such a package and manually employ the package. The compiler will not help you if you make a mistake in manually employing the package, or if you forget to use it, or if you don't know about it.
- atwood22 4y agoHave you seen the runtypes library? It lets you specify your type using a syntax very similar to normal TS type syntax. It then provides parsers for that type that throws errors if the input doesn’t conform to the type. It’s very useful. If anything, the library will impress you with how well it handles arbitrary types as well as how strong the TS type system is.
- copperx 4y agoPlease don't blame Viola for your careless deserializations.