英文:
typescript compiler giving error redeclare block-scoped variable but it is not redeclared
问题
It appears that you're encountering an issue with TypeScript and ESLint related to the redeclaration of the variable 'name'. This error is happening because the 'name' variable is already declared in 'lib.dom.d.ts,' which is a TypeScript declaration file for the DOM.
The problem might be related to TypeScript's module resolution and how it's handling declaration files from node_modules. You may want to try the following solutions:
-
Exclude node_modules: In your tsconfig.json file, you can exclude the 'node_modules' directory from compilation by adding or modifying the "exclude" section:
"exclude": ["node_modules"]
-
Check ESLint Configuration: Ensure that your ESLint configuration is correctly set up to work with TypeScript and that it's not causing any conflicts. Make sure you have ESLint TypeScript plugins installed and configured properly.
-
Avoid Variable Name Conflicts: If possible, consider using a different variable name instead of 'name' to avoid conflicts with existing declarations in 'lib.dom.d.ts.'
Try these steps to resolve the issue and ensure that your TypeScript and ESLint setup is working correctly.
英文:
I just get started with typescript. after creation this line of code const name: string = 'test'
then eslint is yelling at me that
> Cannot redeclare block-scoped variable 'name'.ts(2451)
> lib.dom.d.ts(18092, 15): 'name' was also declared here.
and after cmpilation with tsc I got this error :
> src/app.ts:4:7 - error TS2451: Cannot redeclare block-scoped variable 'name'.
>
> 4 const name: string = 'test'
> ~~~~
>
> C:/Users///*******/npm/node_modules/typescript/lib/lib.dom.d.ts:18092:15
> 18092 declare const name: void;
> ~~~~
> 'name' was also declared here.
checking this file lib.dom.d.ts it seems there is already a variable named
name
. so it seems that tsc is compiling even the files in node modules not just my files
what am I missing ?
I have checked my tsconfig.json file but it seems okay
答案1
得分: 0
I am using a variable name
that clashes with TypeScript global typings because it is already declared in the lib.dom.d.ts
.
Converting the file to an ES module solved the issue:
const name = 'test'
export {} // add this to mark the file as an external module
英文:
it seems , I am using a variable name
that clashes with TypeScript global typings. because it is already declared in the lib.dom.d.ts
.
converting the file to an ES module solved the issue :
const name = 'test'
export {} // add this to mark the file as an external module
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论