Static Analysis - coursehero/ch-react-workshop GitHub Wiki

Eslint

The static code analysis and linting tool ESLint is the de-facto standard for linting JavaScript projects. It includes an excellent support for TypeScript and code formatters.

TypeScript

JavaScript is dynamically typed. That means it doesn't know the type of variable until it is instantiated at run-time. This can be too late.

TypeScript adds type support that helps you find many subtle bugs and types early. Visual Studio Code has excellent support for TypeScript.

Strict null checks

Runtime errors of the form cannot read property 'x' of undefined or undefined is not a function are very commonly caused by bugs in JavaScript code. Out of the box TypeScript already reduces the probability of these kinds of errors occurring, since one cannot use a variable that is not known to the TypeScript compiler (with the exception of properties of any typed variables). It is still possible though to mistakenly utilize a variable that is set to undefined.

With strict null checks enabled (--strictNullChecks compiler flag) the TypeScript compiler will not allow undefined to be assigned to a variable unless you explicitly declare it to be of nullable type. For example, let x : number = undefined will result in a compile error. This fits perfectly with type theory since undefined is not a number. One can define x to be a sum type of number and undefined to correct this: let x : number | undefined = undefined.