[Typescript] 89. Hard - Currying 1
typescript Hard 89
2023-09-14 09:00:44 时间
TypeScript 4.0 is recommended in this challenge
Currying is the technique of converting a function that takes multiple arguments into a sequence of functions that each take a single argument.
For example:
const add = (a: number, b: number) => a + b
const three = add(1, 2)
const curriedAdd = Currying(add)
const five = curriedAdd(2)(3)
The function passed to Currying
may have multiple arguments, you need to correctly type it.
In this challenge, the curried function only accept one argument at a time. Once all the argument is assigned, it should return its result.
/* _____________ Your Code Here _____________ */
type Curried<F> = F extends (...args: infer Args) => infer RT
? Args extends [infer First, ...infer REST]
? REST['length'] extends 0
? (a: First) => RT
: (a: First) => Curried<(...args: REST) => RT>
: () => RT
: never;
declare function Currying<Fn>(fn: Fn): Curried<Fn>
/* _____________ Test Cases _____________ */
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>>,
]
相关文章
- javascript typescript_typescript python
- TypeScript装饰器从入门到应用
- 如何处理TypeScript中的可选项和Undefined
- typescript的泛型_泛型有什么用
- typescript 多继承_说明类的继承和多态有什么作用
- Create Your Own Redux (Typescript)
- 「使用 webpack 5 从0到1搭建React+TypeScript 项目环境」2. 集成 css、less 与 sass
- TypeScript 深水区:3 种类型来源和 3 种模块语法
- TypeScript 中的 any、unknown、never 和 void
- typescript 中的时间处理
- TypeScript | 笔记
- TypeScript 4.8 发布!重点新特性解读
- 12个 Typescript 开发实用技巧清单
- TypeScript 类型体操:合并映射类型的处理结果为联合类型
- (一)为什么要使用 TypeScript(二)安装并配置 Ts
- TypeScript 4.4 RC 发布