Thanks to visit codestin.com
Credit goes to github.com

Skip to content

readonlyNonEmptyArray.union() should accept ReadonlyArray as second argumentΒ #1907

@licarth

Description

@licarth

πŸš€ Feature request

Current Behavior

readonlyNonEmptyArray.union() does not allow ReadonlyArray as second curried argument.

  pipe([1, 2], readonlyNonEmptyArray.union(N.Eq)([])) // does not compile

In my code, I had to do some workaround like this in order to keep the ReadonlyNonEmptyArray type when unioning a ReadonlyNonEmptyArray with a ReadonlyArray

  const doUnion = (nonEmptyArray: ReadonlyNonEmptyArray<number>, array: ReadonlyArray<number>) =>
    pipe(
      array,
      readonlyNonEmptyArray.fromReadonlyArray,
      option.match(
        () => nonEmptyArray,
        (array) => readonlyNonEmptyArray.union(N.Eq)(array)(nonEmptyArray)
      )
    )

EDIT 10.10 : Simpler workaround : use concat and then uniq

const doUnion = (
  nonEmptyArray: ReadonlyNonEmptyArray<number>,
  array: ReadonlyArray<number>,
) =>
  pipe(
    nonEmptyArray,
    readonlyNonEmptyArray.concat(N.Eq)(array),
    readonlyNonEmptyArray.uniq(N.Eq),
  );

Desired Behavior

  pipe([1, 2], readonlyNonEmptyArray.union(N.Eq)([])) // should work fine

and I could rewrite doUnion like this :

  const doUnion = (nonEmptyArray: ReadonlyNonEmptyArray<number>, array: ReadonlyArray<number>) =>
    pipe(nonEmptyArray, readonlyNonEmptyArray.union(N.Eq)(array))

Suggested Solution

Change signature of readonlyNonEmptyArray to accept ReadonlyArray as second.

Who does this impact? Who is this for?

People using readonlyNonEmptyArray

Describe alternatives you've considered

Additional context

Your environment

Software Version(s)
fp-ts 2.15
TypeScript 5.0.4

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