diff --git a/.changeset/curvy-cats-smoke.md b/.changeset/curvy-cats-smoke.md new file mode 100644 index 0000000000..bdd8ad2b3e --- /dev/null +++ b/.changeset/curvy-cats-smoke.md @@ -0,0 +1,5 @@ +--- +"effect": minor +--- + +preserve `Array.modify` `Array.modifyOption` non emptiness diff --git a/packages/effect/dtslint/Array.ts b/packages/effect/dtslint/Array.ts index b29a5f03e8..53ea255d1c 100644 --- a/packages/effect/dtslint/Array.ts +++ b/packages/effect/dtslint/Array.ts @@ -1363,6 +1363,9 @@ pipe(nonEmptyNumbers, Array.replace(0, "a" as const)) // $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)) + // ------------------------------------------------------------------------------------- // replaceOption // ------------------------------------------------------------------------------------- @@ -1390,3 +1393,114 @@ pipe(nonEmptyNumbers, Array.replaceOption(0, "a" as const)) // $ExpectType Option<("a" | 1 | 2)[]> pipe(new Set([1, 2] as const), Array.replaceOption(0, "a" as const)) + +// ------------------------------------------------------------------------------------- +// modify +// ------------------------------------------------------------------------------------- + +// $ExpectType string[] +Array.modify([], 0, ( + _n // $ExpectType never +) => "a") +// $ExpectType (string | number)[] +Array.modify(numbers, 0, ( + _n // $ExpectType number +) => "a") + +// $ExpectType [number | "a", ...(number | "a")[]] +Array.modify(nonEmptyNumbers, 0, ( + _n // $ExpectType number +) => "a" as const) + +// $ExpectType ("a" | 1 | 2)[] +Array.modify(new Set([1, 2] as const), 0, ( + _n // $ExpectType 1 | 2 +) => "a" as const) + +// $ExpectType string[] +pipe( + [], + Array.modify(0, ( + _n // $ExpectType never + ) => "a") +) + +// $ExpectType (string | number)[] +pipe( + numbers, + Array.modify(0, ( + _n // $ExpectType number + ) => "a") +) + +// $ExpectType [number | "a", ...(number | "a")[]] +pipe( + nonEmptyNumbers, + Array.modify(0, ( + _n // $ExpectType number + ) => "a" as const) +) + +// $ExpectType ("a" | 1 | 2)[] +pipe( + new Set([1, 2] as const), + Array.modify(0, ( + _n // $ExpectType 1 | 2 + ) => "a" as const) +) + +// ------------------------------------------------------------------------------------- +// modifyOption +// ------------------------------------------------------------------------------------- + +// $ExpectType Option +Array.modifyOption([], 0, ( + _n // $ExpectType never +) => "a") + +// $ExpectType Option<(string | number)[]> +Array.modifyOption(numbers, 0, ( + _n // $ExpectType number +) => "a") + +// $ExpectType Option<[number | "a", ...(number | "a")[]]> +Array.modifyOption(nonEmptyNumbers, 0, ( + _n // $ExpectType number +) => "a" as const) + +// $ExpectType Option<("a" | 1 | 2)[]> +Array.modifyOption(new Set([1, 2] as const), 0, ( + _n // $ExpectType 1 | 2 +) => "a" as const) + +// $ExpectType Option +pipe( + [], + Array.modifyOption(0, ( + _n // $ExpectType never + ) => "a") +) + +// $ExpectType Option<(string | number)[]> +pipe( + numbers, + Array.modifyOption(0, ( + _n // $ExpectType number + ) => "a") +) + +// $ExpectType Option<[number | "a", ...(number | "a")[]]> +pipe( + nonEmptyNumbers, + Array.modifyOption(0, ( + _n // $ExpectType number + ) => "a" as const) +) + +// $ExpectType Option<("a" | 1 | 2)[]> +pipe( + new Set([1, 2] as const), + Array.modifyOption(0, ( + _n // $ExpectType 1 | 2 + ) => "a" as const) +) diff --git a/packages/effect/src/Array.ts b/packages/effect/src/Array.ts index a8545f19e0..17ce3b9663 100644 --- a/packages/effect/src/Array.ts +++ b/packages/effect/src/Array.ts @@ -1109,8 +1109,15 @@ export const replaceOption: { * @since 2.0.0 */ export const modify: { - (i: number, f: (a: A) => B): (self: Iterable) => Array - (self: Iterable, i: number, f: (a: A) => B): Array + = Iterable>( + i: number, + f: (a: ReadonlyArray.Infer) => B + ): (self: S) => ReadonlyArray.With | B> + = Iterable>( + self: S, + i: number, + f: (a: ReadonlyArray.Infer) => B + ): ReadonlyArray.With | B> } = dual( 3, (self: Iterable, i: number, f: (a: A) => B): Array => @@ -1134,8 +1141,15 @@ export const modify: { * @since 2.0.0 */ export const modifyOption: { - (i: number, f: (a: A) => B): (self: Iterable) => Option> - (self: Iterable, i: number, f: (a: A) => B): Option> + = Iterable>( + i: number, + f: (a: ReadonlyArray.Infer) => B + ): (self: S) => Option | B>> + = Iterable>( + self: S, + i: number, + f: (a: ReadonlyArray.Infer) => B + ): Option | B>> } = dual(3, (self: Iterable, i: number, f: (a: A) => B): Option> => { const out = Array.from(self) if (isOutOfBound(i, out)) {