Cannot preserve error types in nested function calls

Author: tatgeanCreated Jul 10, 2025Updated Mar 4, 2026

Hi, I'm trying to understand this behaviour of neverthrow:

import { err, ok } from "neverthrow";

class F1Error extends Error { }
function f1() {
  if (Math.random() > 0.5) {
    return err(new F1Error());
  }
  return ok(true);
}

class F2Error extends Error { }
function f2() {
  if (Math.random() > 0.5) {
    return err(new F2Error());
  }

  const f1Result = f1();
  if (f1Result.isErr()) {
    return f1Result;
  }

  return ok(true);
}

const finalResult = f2();

Now in vscode or typescript playground, when I hover on finalResult, it gives me Err<unknown, F1Error> | Ok<boolean, unknown>. I think it should be Err<unknown, F1Error> | Err<unknown, F2Error> | Ok<boolean, unknown>? or is it an expected behaviour?

I'm new to both typescript and neverthrow, please correct me if I'm asking a stupid question. Many thanks.

Here is the typescript playground with the code above