I have a very simple example where TypeScript (3.5.1) is fine with the code, but it immediately throws an error when run.
I believe the issue here is that essentially value
is declared but not initialized prior to getValue
running. This is pretty unintuitive imo but I understand this is how JS works.
However, why can't TS detect this issue in such a simple example? Since value
is a const, it seems to me TS should be able to determine exactly when it's set and predict that this code crashes.
console.log(getValue());
const value = "some string";
function getValue() {
return value;
}
In a second example without a function call, TS does catch that the variable is used before assignment:
console.log(value);
const value = "some string";
TSLint's no-use-before-declare also does not appear applicable.
Assuming TS/linting will not be able to catch this, is there a best practice to apply in the initial example that will avoid this crash? "Always declare module-level consts at top of file" for example.