在打字稿中,如何从咖喱函数中第二个函数调用的参数类型推断参数类型

时间:2019-11-22 23:56:37

标签: typescript

如果我有功能

const g = f => p => f(p)
g(x => x + 3)(5)

如何键入'g',以便可以从参数'5'推断出x的类型?

1 个答案:

答案 0 :(得分:0)

按照您的描述方式,这是不可能的,因为工厂g可能会创建不同的功能,而const func = g(x => x + 3);可能会用作func('asdad') // returns 'asdad3'func(5) // returns 8。您无法通过这种用法来推断类型,因为存在无限种用法。您必须先设置x的类型,然后确定它是func('asda')还是func(5)