打字稿-这是什么类型的功能?

时间:2020-07-06 11:15:02

标签: reactjs typescript function

有人可以告诉我:

  1. 什么类型的功能?链接到详细说明吗?
  2. 如何调用提供名称和值参数的方式
private someFunction = (name: string) => (value: number): void => {
    //Do some stuff there
};

谢谢!

2 个答案:

答案 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);