首页 > 其他分享 >[Typescript] 89. Hard - Currying 1

[Typescript] 89. Hard - Currying 1

时间:2022-11-08 03:11:18浏览次数:72  
标签:Typescript const type number boolean 89 Currying true

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>>,
]

 

标签:Typescript,const,type,number,boolean,89,Currying,true
From: https://www.cnblogs.com/Answer1215/p/16868037.html

相关文章

  • [Typescript] 88. Hard - Simple Vue
    ImplementasimpiledversionofaVue-liketypingsupport.Byprovidingafunctionname SimpleVue (similarto Vue.extend or defineComponent),itshouldpr......
  • [Typescript] ThisType
    Thisutilitydoesnotreturnatransformedtype.Instead,itservesasamarkerforacontextual this type.Notethatthe noImplicitThis flagmustbeenabl......
  • Typescript类型体操 - Join
    题目中文实现类型版本的Array.join,Join<T,U>接受数组T和string或者number类型U作为泛型参数,并返回U连接数组T后的字符串.EnglishImplementthetyp......
  • E710八通道读写器7189 Lite
    简介   E710八通道超高频电子标签读写器是一款高性能的UHF超高频电子标签读写器,完全自主知识产权设计,结合专有的高效信号处理算法,在保持高识读率的同时,实现对电子标签的......
  • React中常见的TypeScript定义实战
    一引沿Fiber架构是React16中引入的新概念,目的就是解决大型React应用卡顿,React在遍历更新每一个节点的时候都不是用的真实DOM,都是采用虚拟DOM,所以可以理解成fiber就是R......
  • React-hooks+TypeScript最佳实战
    ReactHooks什么是HooksReact一直都提倡使用函数组件,但是有时候需要使用state或者其他一些功能时,只能使用类组件,因为函数组件没有实例,没有生命周期函数,只有类组件才......
  • TypeScript与JavaScript区别
    TypeScript是JavaScript的一个超集,支持ECMAScript6标准。TypeScript由微软开发的自由和开源的编程语言。TypeScript设计目标是开发大型应用,它可以编译成纯JavaS......
  • 【面试题】 那些你不知道的Typescript面试题
    1.面试官:说说你对TypeScript中类的理解?应用场景?一、是什么类(Class)是面向对象程序设计(OOP,Object-OrientedProgramming)实现信息封装的基础类是一种用户定义的引用数据类型,......
  • typescript 数据类型
    一、是什么typescript 和 javascript几乎一样,拥有相同的数据类型,另外在javascript基础上提供了更加实用的类型供开发使用在开发阶段,可以为明确的变量定义为某种类型,这......
  • [Typescript] Function scope in typescript
    Wehavethefollowingcodewhichhascompileerror:asyncfunctionreadData(event?:Event|unknown):Promise<void>{//...lettext:string|undef......