Javascript 为什么会出现无法重新声明块作用域变量的错误';名称';打字稿?

Javascript 为什么会出现无法重新声明块作用域变量的错误';名称';打字稿?,javascript,typescript,variables,Javascript,Typescript,Variables,我试图在我的typescript代码中声明名为name的新变量。我只有一行代码 var name:string = "John"; 但是有错误 PS D:\TypeScript> tsc test.ts C:/Users/Users/AppData/Roaming/npm/node_modules/typescript/lib/lib.d.ts(18568,15): error TS2451: Cannot re declare block-scoped variable 'name'.

我试图在我的typescript代码中声明名为name的新变量。我只有一行代码

var name:string = "John";
但是有错误

PS D:\TypeScript> tsc test.ts
C:/Users/Users/AppData/Roaming/npm/node_modules/typescript/lib/lib.d.ts(18568,15): error TS2451: Cannot re
declare block-scoped variable 'name'.
test.ts(1,5): error TS2451: Cannot redeclare block-scoped variable 'name'.
这一错误背后的原因是什么


默认情况下,TypeScript为全局执行环境使用DOM类型,并且在DOM的全局窗口中有一个name属性

如果将var名称更改为name1,则不会显示此错误。但是,typescript建议使用ES6语法,因此IDE可能会建议您使用const/let

看看这个链接

希望这有帮助


愉快地学习

要解决这个问题,您只需拥有一个不导出任何内容的导出语句即可。换言之,只要写

 export {};

文件顶层的某个地方。

出现错误是因为变量
name
已存在于全局作用域中。解决方法是将其定义为:
export var name:string=“John”