TypeScript编译器出错,报告重新声明块作用域变量,但实际上并未重新声明。

huangapple go评论57阅读模式
英文:

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:

  1. 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"]
    
  2. 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.

  3. 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

huangapple
  • 本文由 发表于 2023年5月8日 01:05:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195252.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定