Skip to content

Commit

Permalink
preserve Array.replace Array.replaceOption non emptiness (#3491)
Browse files Browse the repository at this point in the history
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
  • Loading branch information
2 people authored and tim-smart committed Aug 30, 2024
1 parent e9d0310 commit d6e7e40
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/rich-pumas-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

preserve `Array.replace` `Array.replaceOption` non emptiness
22 changes: 22 additions & 0 deletions packages/effect/dtslint/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1334,3 +1334,25 @@ Array.getSomes(hole<Iterable<Option.Option<number> | Option.Option<string>>>())

// $ExpectType (string | number)[]
Array.getSomes(hole<Iterable<Option.Option<number>> | Iterable<Option.Option<string>>>())

// -------------------------------------------------------------------------------------
// replace
// -------------------------------------------------------------------------------------

// $ExpectType string[]
Array.replace([], 0, "a")

// $ExpectType ("a" | 1 | 2)[]
Array.replace(new Set([1, 2] as const), 0, "a" as const)

// $ExpectType [number | "a", ...(number | "a")[]]
Array.replace(Array.of(1), 0, "a" as const)

// $ExpectType string[]
pipe([], Array.replace(0, "a"))

// $ExpectType ("a" | 1 | 2)[]
pipe(new Set([1, 2] as const), Array.replace(0, "a" as const))

// $ExpectType [number | "a", ...(number | "a")[]]
pipe(Array.of(1), Array.replace(0, "a" as const))
26 changes: 22 additions & 4 deletions packages/effect/src/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1055,8 +1055,17 @@ export const insertAt: {
* @since 2.0.0
*/
export const replace: {
<B>(i: number, b: B): <A>(self: Iterable<A>) => Array<A | B>
<A, B>(self: Iterable<A>, i: number, b: B): Array<A | B>
<B>(
i: number,
b: B
): <A, S extends Iterable<A> = Iterable<A>>(
self: S
) => ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>
<A, B, S extends Iterable<A> = Iterable<A>>(
self: S,
i: number,
b: B
): ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>
} = dual(3, <A, B>(self: Iterable<A>, i: number, b: B): Array<A | B> => modify(self, i, () => b))

/**
Expand All @@ -1072,8 +1081,17 @@ export const replace: {
* @since 2.0.0
*/
export const replaceOption: {
<B>(i: number, b: B): <A>(self: Iterable<A>) => Option<Array<A | B>>
<A, B>(self: Iterable<A>, i: number, b: B): Option<Array<A | B>>
<B>(
i: number,
b: B
): <A, S extends Iterable<A> = Iterable<A>>(
self: Iterable<A>
) => Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>
<A, B, S extends Iterable<A> = Iterable<A>>(
self: S,
i: number,
b: B
): Option<ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>>
} = dual(
3,
<A, B>(self: Iterable<A>, i: number, b: B): Option<Array<A | B>> => modifyOption(self, i, () => b)
Expand Down

0 comments on commit d6e7e40

Please sign in to comment.