【问题标题】:Declaring curried function in TypeScript在 TypeScript 中声明 curried 函数
【发布时间】:2018-09-07 11:12:58
【问题描述】:

给定一个参数的柯里化函数接口

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.

【问题讨论】:

  • "declare generic function" --- 你的值不应该是特定类型的吗?您应该实例化具有特定类型的泛型类型。例如declare let myFunction: CurriedFunction1&lt;string[], string&gt;
  • 好吧,这行得通:declare function a&lt;T&gt;(): T;declare const b: &lt;T&gt;() =&gt; T;。所以我可以使用泛型函数声明来声明函数,但是如果我不能使用(..)的泛型接口定义函数?

标签: typescript generics currying


【解决方案1】:

感谢您的评论,我现在明白您要做什么了!这是不可能的。调用签名的类型参数列表在接口中声明调用签名时是固定的,不支持在CurriedFunction1&lt;T[], string&gt;等现有类型中添加类型变量的通用量化。最接近的方法是引入一个包装函数,该函数必须使用类型参数调用才能获得实际的CurriedFunction1:

declare let myFunction: <T>() => CurriedFunction1<T[], string>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-31
    • 2019-09-23
    • 1970-01-01
    • 2016-03-13
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多