Constants are block-scoped, much like variables declared using the let keyword. The value of a constant can't be changed through reassignment (i.e. by using the assignment operator), and it can't be redeclared (i.e. through a variable declaration).
First, declare a variable x and initialize its value to 10. Second, declare a new variable with the same name x inside the if block but with an initial value of 20. Third, output the value of the variable x inside and after the if block.
Block scoped variables: A block scoped variable means that the variable defined within a block will not be accessible from outside the block. A block can reside inside a function, and a block scoped variable will not be available outside the block even if the block is inside a function.
Regarding the error itself, let
is used to declare local variables that exist in block scopes instead of function scopes. It's also more strict than var
, so you can't do stuff like this:
if (condition) {
let a = 1;
...
let a = 2;
}
Also note that case
clauses inside switch
blocks don't create their own block scopes, so you can't redeclare the same local variable across multiple case
s without using {}
to create a block each.
As for the import, you are probably getting this error because TypeScript doesn't recognize your files as actual modules, and seemingly model-level definitions end up being global definitions for it.
Try importing an external module the standard ES6 way, which contains no explicit assignment, and should make TypeScript recognize your files correctly as modules:
import * as co from "./co"
This will still result in a compile error if you have something named co
already, as expected. For example, this is going to be an error:
import * as co from "./co"; // Error: import definition conflicts with local definition
let co = 1;
If you are getting an error "cannot find module co"...
TypeScript is running full type-checking against modules, so if you don't have TS definitions for the module you are trying to import (e.g. because it's a JS module without definition files), you can declare your module in a .d.ts
definition file that doesn't contain module-level exports:
declare module "co" {
declare var co: any;
export = co;
}
The best explanation I could get is from Tamas Piro's post.
TLDR; TypeScript uses the DOM typings for the global execution environment. In your case there is a 'co' property on the global window object.
To solve this:
Rename the variable, or
Use TypeScript modules, and add an empty export{}:
export {};
or
Configure your compiler options by not adding DOM typings:
Edit tsconfig.json in the TypeScript project directory.
{
"compilerOptions": {
"lib": ["es6"]
}
}
For those coming here in this age, here is a simple solution to this issue. It at least worked for me in the backend. I haven't checked with the frontend code.
Just add:
export {};
at the top of your code.
Credit to EUGENE MURAVITSKY
I was receiving this similar error when compiling my Node.JS Typescript application:
node_modules/@types/node/index.d.ts:83:15 - error TS2451: Cannot redeclare block-scoped variable 'custom'.
The fix was to remove this:
"files": [
"./node_modules/@types/node/index.d.ts"
]
and to replace it with this:
"compilerOptions": {
"types": ["node"]
}
Use IIFE(Immediately Invoked Function Expression)
, IIFE
(function () {
all your code is here...
})();
I have also dealt with this issue while working with ts on vscode. The way i fix this is easy, but it might not be the same as your problem.
My editor shows this error when i have the typescript file and the javascript file open at the same time in the recent tabs. I just close it and the error goes away.
Hope this helps someone who may also be scratching their head from this simple bug.
The solution for me was to convert my code from using CommonJS (require
, module.exports
, etc) to ES Modules (import
, export
, etc.)
The TypeScript documentation also appears to recommend ES Module syntax: TypeScript Modules
Of course there's a lot more to it than this, but as a start:
Replace instances of require
with import
, e.g.
Replace:
const https = require('https');
With:
import https from 'https';
Replace the default module.exports
with export default
, e.g.
Replace:
module.exports = myVar ...
With:
export default myVar ...
Replace other module.exports
with export
, e.g.
Replace:
module.exports.myVar =
or:
module.export = { myVar ... }
With:
export myVar ...
More here: From CommonJS to ES Modules: How to modernize your Node.js app
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With