Skip to main content

no-unsafe-unwrap

Disallow unsafe Result extraction APIs that can throw at runtime.

Severity: warn | Fixable: code

Rule Details

This rule reports usage of the following methods on Result and ResultAsync values from antithrow:

  • unwrap
  • unwrapErr
  • expect
  • expectErr

These methods can throw (or reject for ResultAsync) when called on the wrong variant. This bypasses explicit error handling and undermines typed error flows.

The rule is type-aware and only reports when the receiver is a Result/ResultAsync from antithrow.

It also reports extracting these members as properties, for example const fn = result.unwrap and const { unwrap } = result.

When the receiver is statically known to be Ok or Err, direct calls are auto-fixable:

  • unwrap on Ok.value
  • unwrapErr on Err.error

Calls to expect/expectErr, mixed or unknown variants (Result<T, E>, ResultAsync, etc.), and member extraction patterns are reported without autofix.

Invalid

import { ok, err, okAsync, errAsync } from "antithrow";

ok(1).unwrap();
err("x").unwrapErr();
ok(1).expect("value should exist");
err("x").expectErr("error should exist");

okAsync(1).unwrap();
errAsync("x").expectErr("error should exist");

const result = ok(1);
const fn = result.unwrap;
const { unwrap } = result;

Autofix Examples

import { ok, err } from "antithrow";

ok(1).unwrap(); // auto-fix -> ok(1).value

err("x").unwrapErr(); // auto-fix -> err("x").error

Valid

import { ok } from "antithrow";

// Use explicit branch handling
ok(1).match({ ok: (v) => v, err: (_e) => 0 });

// Safe fallback APIs are allowed
ok(1).unwrapOr(0);

// Non-antithrow objects are ignored
const box = {
unwrap() {
return 1;
},
};
box.unwrap();

// Local helper names are ignored
function expect(value: number): number {
return value;
}
expect(1);

When Not To Use It

If your codebase intentionally allows unwrap/expect-style APIs and you accept runtime throws as a control-flow mechanism.

Options

This rule has no options.

Type Checking

This rule requires type information to run. You must configure your ESLint setup with parserOptions.projectService or parserOptions.project.