-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhard_currying-1.ts
More file actions
53 lines (48 loc) · 1.22 KB
/
Copy pathhard_currying-1.ts
File metadata and controls
53 lines (48 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// https://typehero.dev/challenge/currying-1
import type { Equal, Expect } from '@type-challenges/utils'
const curried1 = Currying((a: string, b: number, c: boolean) => true)
const curried2 = Currying(
(
a: string,
b: number,
c: boolean,
d: boolean,
e: boolean,
f: string,
g: boolean,
) => true,
)
const curried3 = Currying(() => true)
type cases = [
Expect<
Equal<typeof curried1, (a: string) => (b: number) => (c: boolean) => true>
>,
Expect<
Equal<
typeof curried2,
(
a: string,
) => (
b: number,
) => (
c: boolean,
) => (d: boolean) => (e: boolean) => (f: string) => (g: boolean) => true
>
>,
Expect<Equal<typeof curried3, () => true>>,
]
type RecursiveCurriedFn<Args extends unknown[], Result> = (
args: Args[0],
) => Args extends [Args[0]] ? Result
: RecursiveCurriedFn<
Args extends [unknown, ...infer Rest] ? Rest : never,
Result
>
declare function Currying<Fn>(
// inferring Result through generic widens the type of true to boolean, so instead infer the whole Fn
fn: Fn,
): Fn extends (...args: infer Args) => infer Result ?
Args extends [] ?
() => Result
: RecursiveCurriedFn<Args, Result>
: never