Type Challenges -- TS 练习题之 Hard 篇答案(持续更新)
感谢 type-challenges 提供的题目
前言
建议 fork 原仓库地址在本地进行答题
原仓库地址:type-challenges
答案仓库地址: github.com/fortress-fi…
相关文章
Type Challenges -- TS 练习题之 Easy 篇答案
Type Challenges -- TS 练习题之 Medium 篇答案 (持续更新)
Type Challenges -- TS 练习题之 Hard 篇答案 (持续更新)
hard
00017-hard-currying-1 [柯里化函数类型定义—将带有多个参数的函数转换为每个带有一个参数的函数序列 ]
// #region
// 测试一:失败原因,传递的函数返回值没有收敛.
// 以 curried1 为例,返回值类型为 boolean 而不是测试要求的 true
// type test<F extends (...params: any[]) => any, R> = F extends (
// ...args: [infer K, ...infer J]
// ) => any
// ? (arg: K) => J extends [...any[], any] ? test<(...param: J) => any, R> : R
// : never
// declare function Currying<T extends (...params: any[]) => any>(
// fn: T,
// ): T extends (...args: any[]) => infer R ? test<T, R> : never
// const curried1 = Currying((a: string, b: number, c: boolean) => true)
// #endregion
type Currying<T extends Function> = T extends (...args: infer A) => infer V
? A extends [infer K, ...infer J]
? (arg: K) => Currying<(...param: J) => V>
: V
: never
declare function Currying<T extends Function>(fn: T): Currying<T>
如果在定义泛型时设定返回值为 any 将会在实际使用过程中,将返回值为 true 的定义,转变成返回值为 boolean