typescript - 值被声明并且仅在打字稿中的 if 语句之外使用
问题描述
在打字稿中,我的代码如下:
let a = 'null'
if (condition) {
const a = 'condition was met'
}
const result = getName(a)
const a = 'condition was met'
但是,编译/构建失败,因为 a in say下方有一条黄线a is declared but its value is never read
。有谁知道对此的修复,以便以后可以使用 a 并且我可以在 if 语句中更改它的值?如果我尝试删除,我会在“说let a = 'null'
”下得到一个红色下划线const result = getName(a)
Cannot find name 'a'
解决方案
因为变量 shadowing和if 块let a
内部const a
不一样。
你应该这样写:
let a = 'null'
if (condition) {
a = 'condition was met'
}
const result = getName(a)
推荐阅读
- javascript - 如何根据状态(选中、禁用)设置自定义选择类?
- javascript - 如何使用 Javascript/jQuery 使用新对象制作其他表单?
- python - Pandas 不一致的布尔比较
- cassandra - cassandra“无效的列名类型”
- excel - 使用python,pandas将excel文件的逗号分隔数据转换为新行
- reactjs - 使用 BrowserRouter,如何刷新页面而不在生产服务器上获得 404?
- module - PAKCS REPL 考虑一些未定义的东西,但定义它的模块已加载
- python - 将字典值从元组转换为列表(python)
- javascript - SyntaxError:预期的表达式,回滚后由 Heroku 的 Main.js 导致 '<'
- python - twine 在哪里寻找证书?