I would guess it is because just about every function can throw, and TS can't detect it with certainty, even for functions which are 100% TS. It also doesn't improve transcription or performance.
Why can’t provenance of throws be traced with certainty? It is possible to trace the return type of functions.
Even if it were an opt-in/progressive adoption keyword like “throws” that enforced some compiler guardrails, I think that could be a huge win for reducing unexpected runtime exceptions.
function arrmin<T extends {field: number}>(a: T[]): number {
let m = a[0].field;
for (let i = 1; i < a.length; i++) {
if (a[i].field < m) {
m = a[i].field;
}
}
return m;
}
Now call arrmin([]). There's no way to avoid a throw. You would have to annotate every function that uses field selection on an array element (and a bunch of other conditions) as "throws", or enforce error checking code (if (0 <= i < a.length) {...}).
Rust didn't solve that either, does it? It just panics.
Even if it were an opt-in/progressive adoption keyword like “throws” that enforced some compiler guardrails, I think that could be a huge win for reducing unexpected runtime exceptions.