Skip to main content

@antithrow/no-unsafe-unwrap

Rule id: @antithrow/no-unsafe-unwrap

Reports calls to methods that will throw if the Result is in the wrong variant: unwrap and unwrapErr.

Metadata

FieldValue
Typeproblem
Recommendedwarn
Requires type informationyes
Fixablecode
Has suggestionsno

Options

None.

{ "@antithrow/no-unsafe-unwrap": "warn" }

Message IDs

IDTemplate
unsafeUnwrapAvoid `{{ method }}` on Result values. Handle both branches explicitly instead.
unwrapOkValue`{{ method }}` on `Ok` is unnecessary. Use `.value` instead.
unwrapErrError`{{ method }}` on `Err` is unnecessary. Use `.error` instead.

Auto-fix behavior

When the static type is known to be Ok<...>, calls to .unwrap() are rewritten to .value.

When the static type is known to be Err<...>, calls to .unwrapErr() are rewritten to .error.

All other cases — including any type that includes Pending<...>, where .unwrap() returns a promise instead of a value — are reported without a fix, since removing the throw requires the author to decide how to handle every branch.

Example

const result = computeResult();

// reports (unsafeUnwrap): handle both branches
result.unwrap();

// ok:
if (result.isOk()) {
console.log(result.value);
}

// Destructuring also reports:
const { unwrap } = result; // unsafeUnwrap