loathers / kolmafia-js

This is now managed from the official KoLmafia repository
3 stars 3 forks source link

Let abort return never #4

Closed oxc closed 3 years ago

oxc commented 3 years ago

Since abort immediately returns, it should be declared with return type never. That lets TypeScript smart cast values that have been guarded by an abort, because it knows that function never returns. For example:

  const match = regexp.exec(text);
  if (!match) kolmafia.abort('Expected to find match.');
  const ix = match[1]; // fails --> TS2531: Object is possibly 'null'.

If kolmafia.abort returned never, this would compile as expected, while still being fully type-safe.

(This was the first example I had at hand, let me know if you need a better one.)

gausie commented 3 years ago

Thanks! This is now the case in v1.1.1

oxc commented 3 years ago

Cool, thanks!