From 791a13db316254fd78cb710f56c2db08c45b7fab Mon Sep 17 00:00:00 2001 From: Oliver Joseph Ash Date: Fri, 12 Sep 2025 11:59:48 +0100 Subject: [PATCH 1/2] Add tests --- packages/effect/dtslint/Array.tst.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/packages/effect/dtslint/Array.tst.ts b/packages/effect/dtslint/Array.tst.ts index d19f1d3f117..b87d63aec1e 100644 --- a/packages/effect/dtslint/Array.tst.ts +++ b/packages/effect/dtslint/Array.tst.ts @@ -1,6 +1,7 @@ import { Array, Effect, Either, Option, Order, Predicate } from "effect" import { hole, identity, pipe } from "effect/Function" import { describe, expect, it, when } from "tstyche" +import { NonEmptyReadonlyArray } from "effect/Array" declare const nonEmptyReadonlyStrings: Array.NonEmptyReadonlyArray declare const nonEmptyNumbers: Array.NonEmptyArray @@ -1239,4 +1240,26 @@ describe("Array", () => { }) )).type.toBe<[state: number, mappedArray: [string, ...Array]]>() }) + + it("match", () => { + const array: Array = [1, 2, 3] + const readonlyArray: ReadonlyArray = [1, 2, 3]; + + expect(pipe(array, Array.match({ onEmpty: () => 1, onNonEmpty: (xs) => { + expect(xs).type.toBe>() + return 1 + } }))) + expect(pipe(readonlyArray, Array.match({ onEmpty: () => 1, onNonEmpty: (xs) => { + expect(xs).type.toBe>() + return 1 + } }))) + expect(Array.match(array, { onEmpty: () => 1, onNonEmpty: (xs) => { + expect(xs).type.toBe>() + return 1 + } })) + expect(Array.match(readonlyArray, { onEmpty: () => 1, onNonEmpty: (xs) => { + expect(xs).type.toBe>() + return 1 + } })) + }); }) From 1a1ead9e90f83b88b65d2751b7b36bb88778e40b Mon Sep 17 00:00:00 2001 From: Oliver Joseph Ash Date: Fri, 12 Sep 2025 12:01:18 +0100 Subject: [PATCH 2/2] Update `Array.match` to return `NonEmptyArray` when given `Array` --- packages/effect/dtslint/Array.tst.ts | 7 ++++--- packages/effect/src/Array.ts | 13 +++++++++++++ packages/effect/test/Array.test.ts | 2 +- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/effect/dtslint/Array.tst.ts b/packages/effect/dtslint/Array.tst.ts index b87d63aec1e..3eef7cc2d60 100644 --- a/packages/effect/dtslint/Array.tst.ts +++ b/packages/effect/dtslint/Array.tst.ts @@ -1,7 +1,7 @@ import { Array, Effect, Either, Option, Order, Predicate } from "effect" import { hole, identity, pipe } from "effect/Function" import { describe, expect, it, when } from "tstyche" -import { NonEmptyReadonlyArray } from "effect/Array" +import { NonEmptyArray, NonEmptyReadonlyArray } from "effect/Array" declare const nonEmptyReadonlyStrings: Array.NonEmptyReadonlyArray declare const nonEmptyNumbers: Array.NonEmptyArray @@ -1246,15 +1246,16 @@ describe("Array", () => { const readonlyArray: ReadonlyArray = [1, 2, 3]; expect(pipe(array, Array.match({ onEmpty: () => 1, onNonEmpty: (xs) => { - expect(xs).type.toBe>() + expect(xs).type.toBe>() return 1 } }))) + // TODO: not sure what to do about this expect(pipe(readonlyArray, Array.match({ onEmpty: () => 1, onNonEmpty: (xs) => { expect(xs).type.toBe>() return 1 } }))) expect(Array.match(array, { onEmpty: () => 1, onNonEmpty: (xs) => { - expect(xs).type.toBe>() + expect(xs).type.toBe>() return 1 } })) expect(Array.match(readonlyArray, { onEmpty: () => 1, onNonEmpty: (xs) => { diff --git a/packages/effect/src/Array.ts b/packages/effect/src/Array.ts index af8e7d8b989..895b2b88fbf 100644 --- a/packages/effect/src/Array.ts +++ b/packages/effect/src/Array.ts @@ -237,12 +237,25 @@ export const fromOption: (self: Option.Option) => Array = Option.toArra * @since 2.0.0 */ export const match: { + ( + options: { + readonly onEmpty: LazyArg + readonly onNonEmpty: (self: NonEmptyArray) => C + } + ): (self: Array) => B | C ( options: { readonly onEmpty: LazyArg readonly onNonEmpty: (self: NonEmptyReadonlyArray) => C } ): (self: ReadonlyArray) => B | C + ( + self: Array, + options: { + readonly onEmpty: LazyArg + readonly onNonEmpty: (self: NonEmptyArray) => C + } + ): B | C ( self: ReadonlyArray, options: { diff --git a/packages/effect/test/Array.test.ts b/packages/effect/test/Array.test.ts index 5497104b759..bc18f20357e 100644 --- a/packages/effect/test/Array.test.ts +++ b/packages/effect/test/Array.test.ts @@ -1065,7 +1065,7 @@ describe("Array", () => { }) it("match", () => { - const len: (as: ReadonlyArray) => number = Arr.match({ + const len: (as: Array) => number = Arr.match({ onEmpty: () => 0, onNonEmpty: (as) => 1 + len(as.slice(1)) })