Currying 1
约 249 字小于 1 分钟
2026-02-11
题目
在此挑战中建议使用TypeScript 4.0
柯里化 是一种将带有多个参数的函数转换为每个带有一个参数的函数序列的技术。
例如:
const add = (a: number, b: number) => a + b
const three = add(1, 2)
const curriedAdd = Currying(add)
const five = curriedAdd(2)(3)传递给 Currying 的函数可能有多个参数,您需要正确输入它的类型。
在此挑战中,柯里化后的函数每次仅接受一个参数。接受完所有参数后,它应返回其结果。
解题思路
待补充
答案
declare function Currying(fn: any): any验证
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>>,
]参考
无
