문제 설명
거짓 긍정 "연결할 수 없는 코드가 감지되었습니다.ts(7027)"? (False positive "Unreachable code detected.ts(7027)"?)
다음 스니펫은 console.log()
에서 Unreachable code detected.ts(7027)
를 반환합니다. Visual Studio Code v1.43.2
에서 실행 중입니다.
6
const foo = (() => {
try {
// operation like "return fs.readFileSync("./credentials.json")" is more likely to fail (nodejs)
// following line is just placeholder
return { val: 4 };
} catch (err) {
return { val: ‑1 };
}
})();
console.log(foo.val); //vs throws unreachable
Is it really a false positive?
참조 솔루션
방법 1:
If you really wanted to implement it this way, you'd need to have some sort of fallback for val
if the catch occurs.
Implement it this way:
const foo = (() => {
let val = undefined
try {
val = 4
} catch (err) {
throw new Error(err);
}
return { val }
})();
console.log(foo.val);
It may be just an issue with TypeScript, but this will fix the problem. that TS is moaning about.
(by IGLAK、Barry Michael Doyle)