diff --git a/.changeset/wise-adults-give.md b/.changeset/wise-adults-give.md new file mode 100644 index 00000000000..61e385cfedf --- /dev/null +++ b/.changeset/wise-adults-give.md @@ -0,0 +1,5 @@ +--- +"effect": minor +--- + +Support multiple values in Function.apply diff --git a/packages/effect/dtslint/Function.tst.ts b/packages/effect/dtslint/Function.tst.ts index 06b74b2a332..d5fbcc8c9b6 100644 --- a/packages/effect/dtslint/Function.tst.ts +++ b/packages/effect/dtslint/Function.tst.ts @@ -1,4 +1,4 @@ -import { flow, identity, Option, pipe } from "effect" +import { flow, Function, identity, Option, pipe } from "effect" import { describe, it } from "tstyche" describe("Function", () => { @@ -27,4 +27,27 @@ describe("Function", () => { ) }) }) + + it("apply", () => { + const apply1 = Function.apply("a") + const apply2 = Function.apply("a", 1) + + const countArgs = (...args: Array) => args.length + const arg1 = (a: string) => a + const arg2 = (a: string, b: number) => `${a}${b}` + const arg3 = (a: number) => a + + const _a1: number = apply1(countArgs) + const _a2: string = apply1(arg1) + // @ts-expect-error: Target signature provides too few arguments. Expected 2 or more, but got 1. + apply1(arg2) + // @ts-expect-error: Type 'string' is not assignable to type 'number'. + apply1(arg3) + + const _b1: number = apply2(countArgs) + const _b2: string = apply2(arg1) + const _b3: string = apply2(arg2) + // @ts-expect-error: Type 'string' is not assignable to type 'number'. + apply1(arg3) + }) }) diff --git a/packages/effect/src/Function.ts b/packages/effect/src/Function.ts index f8e228a5e95..69d53fd2fdd 100644 --- a/packages/effect/src/Function.ts +++ b/packages/effect/src/Function.ts @@ -171,7 +171,7 @@ export const dual: { } } /** - * Apply a function to a given value. + * Apply a function to given values. * * @example * ```ts @@ -184,7 +184,7 @@ export const dual: { * * @since 2.0.0 */ -export const apply = (a: A) => (self: (a: A) => B): B => self(a) +export const apply = >(...a: A) => (self: (...a: A) => B): B => self(...a) /** * A lazy argument. diff --git a/packages/effect/test/Function.test.ts b/packages/effect/test/Function.test.ts index eb430730d36..bfca4987749 100644 --- a/packages/effect/test/Function.test.ts +++ b/packages/effect/test/Function.test.ts @@ -7,7 +7,10 @@ const g = (n: number) => n * 2 describe("Function", () => { it("apply", () => { - deepStrictEqual(Function.pipe(String.length, Function.apply("a")), 1) + const countArgs = (...args: Array) => args.length + + deepStrictEqual(Function.pipe(countArgs, Function.apply("a")), 1) + deepStrictEqual(Function.pipe(countArgs, Function.apply("a", "b", "c")), 3) }) it("compose", () => {