如果我有功能
const g = f => p => f(p)
g(x => x + 3)(5)
如何键入'g',以便可以从参数'5'推断出x的类型?
答案 0 :(得分:0)
按照您的描述方式,这是不可能的,因为工厂g
可能会创建不同的功能,而const func = g(x => x + 3);
可能会用作func('asdad') // returns 'asdad3'
和func(5) // returns 8
。您无法通过这种用法来推断类型,因为存在无限种用法。您必须先设置x
的类型,然后确定它是func('asda')
还是func(5)
。