How to annotate nested functions with generic type arguments

北战南征 提交于 2020-01-06 01:15:32

问题


Given a function that returns a factory, how can I annotate the function/factory so that it contains the correct type definitions?

Here's my example:

class item<TA, TB> {
  constructor(a: TA, b: TB) {
    this.a = a;
  }
  a: T
}

function generate(c) {
  return function a(a) {
    return function b(b) {
      return new c(a, b);
    }
  }
}

const factory = generate(item); // I want this to always be annotated as <TA, TB> (a: TA) => (b: TB) => item<TA, TB>

const partial = factory('string'); // instance should now be of type <TB> (b: TB) => item<string, TB>

const instance = partial('string'); // instance should now be of type item<string, string>

Is this possible in typescript or should I suggest it as a new feature?

来源:https://stackoverflow.com/questions/52931628/how-to-annotate-nested-functions-with-generic-type-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!