我有這樣的代碼:如何獲得TypeScript來確定我的函數不可能返回?
function A(): never {
throw new Error("fail");
}
function B(): never {
A();
}
而且我得到這個錯誤:
index.ts(5,36): error TS2534: A function returning 'never' cannot have a reachable end point.
爲什麼會出現這個錯誤?顯然,A
從不返回,因此B
沒有可達的終點。