Examples will never be of a function that always throws an error or one containing an infinite loop.
Never means never, it simply does not happen.
But throwing an error is not just void
or undefined
, it falls under the category of never
So can we not utilize never in a union? It may sound illogical at first glance, but upon further reflection, it makes sense. Ultimately, it provides precise context, doesn't it?
foo(input: string): number | never {
if (input === 'bazz') {
return 5;
}
throw new Error('could not do it');
}