值被声明并且仅在打字稿中的 if 语句之外使用
value being declared and only used outside if statement in typescript
在打字稿中我有如下代码:
let a = 'null'
if (condition) {
const a = 'condition was met'
}
const result = getName(a)
但是,compilation/build 失败了,因为 const a = 'condition was met'
中的 a 下方有一条黄线表示 a is declared but its value is never read
。任何人都知道解决这个问题,以便以后可以使用 a 并且我可以在 if 语句中更改它的值吗?如果我尝试删除 let a = 'null'
,然后我会在 const result = getName(a)
中的 a 下方看到一条红色下划线,表示 Cannot find name 'a'
因为 if 块中的 variable shadowing、let a
和 const a
不相同。
你应该这样写:
let a = 'null'
if (condition) {
a = 'condition was met'
}
const result = getName(a)
的文档
在打字稿中我有如下代码:
let a = 'null'
if (condition) {
const a = 'condition was met'
}
const result = getName(a)
但是,compilation/build 失败了,因为 const a = 'condition was met'
中的 a 下方有一条黄线表示 a is declared but its value is never read
。任何人都知道解决这个问题,以便以后可以使用 a 并且我可以在 if 语句中更改它的值吗?如果我尝试删除 let a = 'null'
,然后我会在 const result = getName(a)
中的 a 下方看到一条红色下划线,表示 Cannot find name 'a'
因为 if 块中的 variable shadowing、let a
和 const a
不相同。
你应该这样写:
let a = 'null'
if (condition) {
a = 'condition was met'
}
const result = getName(a)
的文档