在 TypeScript 中声明柯里化函数
Declaring curried function in TypeScript
带一个参数的柯里化函数的给定接口
interface CurriedFunction1<T1, R> {
(): CurriedFunction1<T1, R>
(t1: T1): R
}
如何声明该类型的泛型函数?
以下声明无效:
declare let myFunction: CurriedFunction1<T[], string> // Cannot find name 'T'.
declare let myFunction2:<T>CurriedFunction1<T[], string> // '( expected.
感谢您的评论,我现在明白您要做什么了!这是不可能的。调用签名的类型参数列表在接口中声明调用签名时是固定的,不支持为现有类型添加类型变量的通用量化,例如CurriedFunction1<T[], string>
。最接近的方法是引入一个包装函数,该函数必须使用类型参数调用才能获得实际的 CurriedFunction1
:
declare let myFunction: <T>() => CurriedFunction1<T[], string>
带一个参数的柯里化函数的给定接口
interface CurriedFunction1<T1, R> {
(): CurriedFunction1<T1, R>
(t1: T1): R
}
如何声明该类型的泛型函数?
以下声明无效:
declare let myFunction: CurriedFunction1<T[], string> // Cannot find name 'T'.
declare let myFunction2:<T>CurriedFunction1<T[], string> // '( expected.
感谢您的评论,我现在明白您要做什么了!这是不可能的。调用签名的类型参数列表在接口中声明调用签名时是固定的,不支持为现有类型添加类型变量的通用量化,例如CurriedFunction1<T[], string>
。最接近的方法是引入一个包装函数,该函数必须使用类型参数调用才能获得实际的 CurriedFunction1
:
declare let myFunction: <T>() => CurriedFunction1<T[], string>