Skip to content

Commit fa10f56

Browse files
thewilkybarkidtim-smart
authored andcommitted
Support multiple values in Function.apply (#4705)
1 parent 59d24d0 commit fa10f56

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

.changeset/wise-adults-give.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": minor
3+
---
4+
5+
Support multiple values in Function.apply

packages/effect/dtslint/Function.tst.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { flow, identity, Option, pipe } from "effect"
1+
import { flow, Function, identity, Option, pipe } from "effect"
22
import { describe, it } from "tstyche"
33

44
describe("Function", () => {
@@ -27,4 +27,27 @@ describe("Function", () => {
2727
)
2828
})
2929
})
30+
31+
it("apply", () => {
32+
const apply1 = Function.apply("a")
33+
const apply2 = Function.apply("a", 1)
34+
35+
const countArgs = (...args: Array<unknown>) => args.length
36+
const arg1 = (a: string) => a
37+
const arg2 = (a: string, b: number) => `${a}${b}`
38+
const arg3 = (a: number) => a
39+
40+
const _a1: number = apply1(countArgs)
41+
const _a2: string = apply1(arg1)
42+
// @ts-expect-error: Target signature provides too few arguments. Expected 2 or more, but got 1.
43+
apply1(arg2)
44+
// @ts-expect-error: Type 'string' is not assignable to type 'number'.
45+
apply1(arg3)
46+
47+
const _b1: number = apply2(countArgs)
48+
const _b2: string = apply2(arg1)
49+
const _b3: string = apply2(arg2)
50+
// @ts-expect-error: Type 'string' is not assignable to type 'number'.
51+
apply1(arg3)
52+
})
3053
})

packages/effect/src/Function.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export const dual: {
171171
}
172172
}
173173
/**
174-
* Apply a function to a given value.
174+
* Apply a function to given values.
175175
*
176176
* @example
177177
* ```ts
@@ -184,7 +184,7 @@ export const dual: {
184184
*
185185
* @since 2.0.0
186186
*/
187-
export const apply = <A>(a: A) => <B>(self: (a: A) => B): B => self(a)
187+
export const apply = <A extends ReadonlyArray<unknown>>(...a: A) => <B>(self: (...a: A) => B): B => self(...a)
188188

189189
/**
190190
* A lazy argument.

packages/effect/test/Function.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ const g = (n: number) => n * 2
77

88
describe("Function", () => {
99
it("apply", () => {
10-
deepStrictEqual(Function.pipe(String.length, Function.apply("a")), 1)
10+
const countArgs = (...args: Array<unknown>) => args.length
11+
12+
deepStrictEqual(Function.pipe(countArgs, Function.apply("a")), 1)
13+
deepStrictEqual(Function.pipe(countArgs, Function.apply("a", "b", "c")), 3)
1114
})
1215

1316
it("compose", () => {

0 commit comments

Comments
 (0)