有人可以告诉我:
private someFunction = (name: string) => (value: number): void => {
//Do some stuff there
};
谢谢!
答案 0 :(得分:0)
您所拥有的是一个可以返回另一个函数的函数。两者都使用箭头函数语法。
这是相同的,但不是缩写格式
const someFunction = (name: string) => {
return (value: number) => {
console.log(name);
console.log(value);
};
}
const logNameHenryAndValue = someFunction("Henry");
logNameHenryAndValue(2);
一旦logNameHenryAndValue(2)
运行,它应该登录
- "Henry"
- 2
这称为currying,可用于编写函数:What is 'Currying'?
答案 1 :(得分:0)
函数类型为:
type ReturnFuction = (name:string) => ((value: number) => void);
此模式称为currying。
const name = "a";
const value = 5;
someFunction(name)(value);
完整示例:
type ReturnFuction = (name:string) => ((value: number) => void);
const someFunction:ReturnFuction = (name) => (value) => {
//Do some stuff there
};
// someFunction returns a function (number => void)
const foo = someFunction("hello");
// function call (number => void)
foo(5);