首页 > 其他分享 >[Typescript] 143. Extreme - Currying 2

[Typescript] 143. Extreme - Currying 2

时间:2022-12-23 15:55:16浏览次数:54  
标签:curried2 Typescript const 143 123 Expect Currying false true

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

 

标签:curried2,Typescript,const,143,123,Expect,Currying,false,true
From: https://www.cnblogs.com/Answer1215/p/17000851.html

相关文章

  • TypeScript 项目引用简化
    为了避免出现长路径引用:  建议修改成如下:  项目的目录:  这时候,引用就很方便了: ......
  • typescript_01搭建环境
    typescript是什么?以JavaScript为基础构建的语言,可以在任何支持JavaScript的平台中执行,ts不能被js解析器直接执行需要先编译成js文件,ts是一个js的超集,拓展了js并添加了类型......
  • typescript02_声明变量
    //声明一个变量a同时声明他的类型为numberleta:number//a的类型生命为number,在以后的使用过程中a只能是数字a=1//a='hello'此行代码会报错,因为a的类型是number不能......
  • [Typescript] 142. Extreme - Multiply
    Thischallengecontinuesfrom 476-Sum,itisrecommendedthatyoufinishthatonefirst,andmodifyyourcodebasedonittostartthischallenge.Implement......
  • TypeScript 前端工程最佳实践
     作者:王春雨前言随着前端工程化的快速发展,TypeScript变得越来越受欢迎,它已经成为前端开发人员必备技能。TypeScript最初是由微软开发并开源的一种编程语言,自2012年10月......
  • webstorm typescript .d.ts文件 使用问题
    问题描述 webstorm中global.d.ts文件当全局变量用,不行。直接上干货,能对上你的问题就恭喜了。global.d.ts内容  tsconfig.json   直接使用,不用导入  ......
  • [Typescript] 140. Extreme - Integers Comparator
    Implementatype-levelintegerscomparator.We'veprovidedanenumforindicatingthecomparisonresult,likethis:If a isgreaterthan b,typeshouldbe......
  • Typescript类型体操 - Combination
    题目中文给定一个字符串数组,实现它的全排列组合.EnglishGivenanarrayofstrings,doPermutation&Combination.It'salsousefulfortheproptypeslikevid......
  • 源码解读之TypeScript类型覆盖检测工具type-coverage
    因为团队内部开启了一个持续的前端代码质量改进计划,其中一个专项就是TS类型覆盖率,期间用到了type-coverage这个仓库,所以借这篇文章分享一下这个工具,并顺便从源码阅读的角......
  • 143 Linux 批量修改文件夹所有文件指定内容并还原文件修改时间
    143Linux批量修改文件夹所有文件指定内容并还原文件修改时间#!/bin/bashfunction log_date(){    # 获取文件的原始修改时间,并写入同级文件夹下cos_log_date.......