[Typescript] 143. Extreme - Currying 2
Currying is the technique of converting a function that takes multiple arguments into a sequence of functions that each take a single argument.
But in our daily life, currying dynamic arguments is also commonly used, for example, the Function.bind(this, [...params])
API.
const func = (a: number, b: number, c: number) => {
return a + b + c
}
const bindFunc = func(null, 1, 2)
const result = bindFunc(3) // result: 6
Thus, based on Currying 1
, we would need to have the dynamic argument version:
const add = (a: number, b: number, c: number) => a + b + c
const three = add(1, 1, 1)
const curriedAdd = DynamicParamsCurrying(add)
const six = curriedAdd(1, 2, 3)
const seven = curriedAdd(1, 2)(4)
const nine = curriedAdd(2)(3)(4)
In this challenge, DynamicParamsCurrying
may take a function with zero to multiple arguments, you need to correctly type it. The returned function may accept at least one argument. When all the arguments as satisfied, it should yield the return type of the original function correctly.
/* _____________ Your Code Here _____________ */
declare function DynamicParamsCurrying<T extends any[], R>(fn: (...args: T) => R):
T extends [] ? R : // returns R if no params is needed
<P extends any[]>(...args: P) =>
T extends [...P, ...infer K3] ? // check does P & K3 extends T, basically checking is P fully T
ReturnType<typeof DynamicParamsCurrying<K3, R>> : // Pass in K3 and T to check is K3 = T
R;
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
const curried1 = DynamicParamsCurrying((a: string, b: number, c: boolean) => true)
const curried2 = DynamicParamsCurrying((a: string, b: number, c: boolean, d: boolean, e: boolean, f: string, g: boolean) => true)
const curried3 = DynamicParamsCurrying(() => 123)
const curried1Return1 = curried1('123')(123)(true)
const curried1Return2 = curried1('123', 123)(false)
const curried1Return3 = curried1('123', 123, true)
const curried2Return1 = curried2('123')(123)(true)(false)(true)('123')(false)
const curried2Return2 = curried2('123', 123)(true, false)(true, '123')(false)
const curried2Return3 = curried2('123', 123)(true)(false)(true, '123', false)
const curried2Return4 = curried2('123', 123, true)(false, true, '123')(false)
const curried2Return5 = curried2('123', 123, true)(false)(true)('123')(false)
const curried2Return6 = curried2('123', 123, true, false)(true, '123', false)
const curried2Return7 = curried2('123', 123, true, false, true)('123', false)
const curried2Return8 = curried2('123', 123, true, false, true)('123')(false)
const curried2Return9 = curried2('123', 123, true, false, true, '123')(false)
const curried2Return10 = curried2('123', 123, true, false, true, '123', false)
const curried3Return = curried3
type cases = [
Expect<Equal< typeof curried1Return1, boolean>>,
Expect<Equal< typeof curried1Return2, boolean>>,
Expect<Equal< typeof curried1Return3, boolean>>,
Expect<Equal< typeof curried2Return1, boolean>>,
Expect<Equal< typeof curried2Return2, boolean>>,
Expect<Equal< typeof curried2Return3, boolean>>,
Expect<Equal< typeof curried2Return4, boolean>>,
Expect<Equal< typeof curried2Return5, boolean>>,
Expect<Equal< typeof curried2Return6, boolean>>,
Expect<Equal< typeof curried2Return7, boolean>>,
Expect<Equal< typeof curried2Return8, boolean>>,
Expect<Equal< typeof curried2Return9, boolean>>,
Expect<Equal< typeof curried2Return10, boolean>>,
Expect<Equal< typeof curried3Return, number>>,
]
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2020-12-23 [Unit Testing Java] Unit testing with Mockito vs. integration testing
2020-12-23 [Spring Java] identify transactional business services boundaries
2018-12-23 [Algorithms] Refactor a Linear Search into a Binary Search with JavaScript
2016-12-23 [Javascript] Limit Built Branches on Travis
2016-12-23 [RxJS] Split an RxJS Observable into groups with groupBy