Skip to content

Left and Right don't unify #128

@DylanRJohnston

Description

@DylanRJohnston
import {Either, Left, Right} from 'fp-ts/lib/either'

const foo = (x: number) => x < 5 ? Right.of(x) : Left.of("Not less than 5")
const bar = (x: number) => x > 2 ? Right.of(x) : Left.of("Not greater than 2")

Inferred type of foo

const foo: (x: number) => Left<{}, number> | Right<{}, number> | Left<{}, string> | Right<{}, string>

Trying to add return type

const foo = (x: number): Either<string, number> => x < 5 ? Right.of(x) : Left.of("Not less than 5")

Produces the error

[ts]
Type 'Left<string, number> | Right<string, number> | Left<string, string> | Right<string, string>' is not assignable to type 'Either<string, number>'.
  Type 'Left<string, string>' is not assignable to type 'Either<string, number>'.
    Type 'Left<string, string>' is not assignable to type 'Left<string, number>'.
      Type 'string' is not assignable to type 'number'.

This happens in both 2.3.4 and 2.4.0. But given improvements in the type checker in 2.4.0 I thought there may be a way to potentially fix. Not sure where to get started though.

Relevant changes to the typechecker in 2.4.0
microsoft/TypeScript#16072
microsoft/TypeScript#16305
microsoft/TypeScript#16368

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions