变量声明 TypeScript

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

Variable declaration Typescript

问题

无法重新声明具有范围“块”的变量'greet'。我开始学习TypeScript,当声明变量和变量类型并运行以查看结果时,它返回错误-无法重新声明具有范围“块”的变量'greet'。

英文:

Cannot re-declare variable 'greet' with scope 'Block'.

I started learning Typescript, and when declared variable and variable type and launched to see result, it returned error

  • Cannot re-declare variable 'greet' with scope 'Block'.

变量声明 TypeScript

答案1

得分: 0

将您的文件转换为ES模块,就像这样:

// ✅ 变量成功声明
const name = '编程之美';

console.log(name); // 编程之美

export {};

或者

您可以使用带有花括号的嵌套块来实现这一点:

let color = '红色';

{
  let color = '黄色';

  console.log(color); // 黄色
}

console.log(color); // 红色
英文:

Convert your file to an ESModule, like this:

// ✅ variable declared successfully
const name = 'Coding Beauty';

console.log(name); // Coding Beauty

export {};

Or

You can do this by using nested block designated with curly braces:

let color = 'red';

{
  let color = 'yellow';

  console.log(color); // yellow
}

console.log(color); // red

huangapple
  • 本文由 发表于 2023年5月10日 19:58:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218130.html
匿名

发表评论

匿名网友

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

确定