Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve Array.modify and Array.modifyOption non emptiness #3496

8 changes: 4 additions & 4 deletions packages/effect/dtslint/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1371,10 +1371,10 @@ Array.modify(new Set([1, 2] as const), 0, () => "a" as const)
// $ExpectType Option<("a" | 1 | 2)[]>
Array.modifyOption(new Set([1, 2] as const), 0, () => "a" as const)

// $ExpectType [number | "a", ...(number | "a")[]]
Array.modify(Array.of(1), 0, () => "a" as const)
// $ExpectType Option<[number | "a", ...(number | "a")[]]>
Array.modifyOption(Array.of(1), 0, () => "a" as const)
// $ExpectType [number | string, ...(number | string)[]]
Array.modify(Array.of(1), 0, (n) => n.toString())
// $ExpectType Option<[number | string, ...(number | string)[]]>
Array.modifyOption(Array.of(1), 0, (n) => n.toString())

// $ExpectType string[]
pipe([], Array.modify(0, () => "a"))
Expand Down
6 changes: 4 additions & 2 deletions packages/effect/src/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,8 @@ export const replaceOption: {
<A, B>(self: Iterable<A>, i: number, b: B): Option<Array<A | B>> => modifyOption(self, i, () => b)
)

type TypeOfIterable<T extends Iterable<any>> = T extends Iterable<infer A> ? A : never
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how I fixed the inference of a. Tell me if:

  • it's a valid way to fix the issue
  • it's the correct place to put this kind of the type helpers (or if it already exists somewhere. I did not find anything on my own)


/**
* Apply a function to the element at the specified index, creating a new `Array`,
* or return a copy of the input if the index is out of bounds.
Expand All @@ -1118,7 +1120,7 @@ export const modify: {
<A, B, S extends Iterable<A> = Iterable<A>>(
self: S,
i: number,
f: (a: A) => B
f: (a: TypeOfIterable<S>) => B
): ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>
} = dual(
3,
Expand Down Expand Up @@ -1150,7 +1152,7 @@ export const modifyOption: {
<A, B, S extends Iterable<A> = Iterable<A>>(
self: S,
i: number,
f: (a: A) => B
f: (a: TypeOfIterable<S>) => B
): Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>
} = dual(3, <A, B>(self: Iterable<A>, i: number, f: (a: A) => B): Option<Array<A | B>> => {
const out = Array.from(self)
Expand Down