TS 中的递归类型推断

作者:xie392地址:https://v.douyin.com/ieQtfv3e/更新时间:2024-12-21

案例代码

1
type Curried<T, K> = T extends []
2
? () => K
3
: T extends [infer AGR]
4
? (params: AGR) => K
5
: T extends [infer AGR, ...infer REST]
6
? (params: AGR) => Curried<REST, K>
7
: never
8
9
declare function curry<T extends any[], K>(fn: (...args: T) => K): Curried<T, K>
10
11
function sum(a: string, b: number, c:object) {
12
return a + b
13
}
14
15
const curryFn = curry(sum)
16
17
curryFn('sss')(333)({a:1})