I'm a newcomer in Javascript. I have two files with js code. file a.js contains
const words = ["Alpha", "Bita", "Gama"];
console.log(words);
file b.js contains
//@ts-check
const words = ["Alpha", "Bita", "Gama"];
console.log(words);
Both a.js and b.js are located in folder src under the root In the root, I also have a jsconfig.json
{
"compilerOptions": {
"outDir": "./built",
"module": "commonjs",
"target": "es6",
},
"exclude": [
"node_modules",
"**/node_modules/*"
]
}
In file b.js, I receive an error "Cannot redeclare block-scoped variable 'words'." When I comment out the declaration of words in b.js, I don't receive any error, but when running file b.js, I receive a "ReferenceError: words is not defined".
I know that erasing the //@ts-check
in b.js will fix the problem, but I cannot understand why type checking produces this behaviour. What is the meaning to keep a variable in scope if you are not allowed to access it?
I use Visual Studio Code and run with node.