Skip to content Skip to sidebar Skip to footer

Typesafety In Javascript Or How To Avoid Pretty Hard To Detect Type Related Errors

I come from the Java world, i.e. a typesafe world and am right now doing a couple of things for which I need client side execution using javascript. I keep running into pretty hard

Solution 1:

There are various linting tools which you can use to scan your javascript files before you actually use them. The popular ones in the industry are JSLint, JSHint, JSCS and ESLint.

They come inbuilt with various rule sets which you can configure and you can also add your own rules.

You can compare these to checkstyles and PMD from the JAVA world.


Solution 2:

You have a number of answers. But first, need to clarify: Java is not type-safe (see: NullPointerException, history of).

But to get closer to type-safety in any dynamic language you have the option to pepper your code with asserts. This can to some degree be automated, it may cause performance issues. This is the route I usually take, but I certainly wouldn't do it with three.js.

For JavaScript specifically, you have two additional options: TypeScript and Flow.

TypeScript is a dialect of JavaScript with type annotations that gets compiled down to plain JS. Flow is a static analyzer written in OCaml that tries to infer types in your JS code and check them.


Post a Comment for "Typesafety In Javascript Or How To Avoid Pretty Hard To Detect Type Related Errors"