In the past, I had a library included in my project that I later removed (deleted from package.json
). However, this library had a peer dependency on fp-ts
, so I had to directly add fp-ts
to my project. The fp-ts
library includes an Either
type which allows for checking left/right values:
export declare const isLeft: <E>(ma: Either<E, unknown>) => ma is Left<E>
When I use this in an if
statement like so:
if (E.isLeft(result)) {
// ...
}
then TypeScript correctly infers the type in the else
block to be a right
value.
However, after moving the dependency to be directly included in my project instead of just as a peer dependency, I encountered an issue where the following case no longer works, resulting in a compiler error:
const fail = (msg: string): never => {
throw new GenericProgramError(msg);
};
if (E.isLeft(result)) {
fail("Expected a successful result");
}
expect(result.right).toEqual(
// ^^^--- Property 'right' does not exist on type 'Left'
// ...
);
The problem arises from the fact that if result
is a Left
, the fail
function is called, which returns a type of never
(throws an error). Therefore, TypeScript should be able to infer that in the expect
statement, result
can only have a right
value and not a left
. This was functioning correctly before. What adjustments do I need to make to resolve this issue?