[英]in typescript, how to infer the parameter type from the argument type of the 2nd function call in a curried function
if i have a function如果我有一个功能
const g = f => p => f(p)
g(x => x + 3)(5)
How do I type 'g' so that the type of x can be inferred from the argument '5'?如何键入“g”以便可以从参数“5”推断出 x 的类型?
In the way you are describing it, it's impossible as your factory g
might creat different functions as const func = g(x => x + 3);
按照您描述的方式,这是不可能的,因为您的工厂
g
可能会创建不同的函数,如const func = g(x => x + 3);
may be used as func('asdad') // returns 'asdad3'
and func(5) // returns 8
.可以用作
func('asdad') // returns 'asdad3'
和func(5) // returns 8
。 You cannot infer the type from usage this way as there are endless possibilities of usage.您无法通过这种方式从使用中推断出类型,因为存在无限的使用可能性。 You have to apriori set the type of the
x
thus deciding whether it's gonna be func('asda')
or func(5)
.您必须先验地设置
x
的类型,从而决定它是func('asda')
还是func(5)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.