1

Could someone tell me:

  1. what type of function is it? Link to deep description?
  2. how to call that providing both name and value params
private someFunction = (name: string) => (value: number): void => {
    //Do some stuff there
};

Thanks in advance!

pabloxerr
  • 21
  • 1
  • 5
  • See https://en.wikipedia.org/wiki/Currying and https://en.wikipedia.org/wiki/Partial_application . To call `someFunction('foo')(123)` – Aleksey L. Jul 06 '20 at 11:32

2 Answers2

1

What you have there is a function that returns another function. Both using the arrow function syntax.

This is the same but then not in the shortened format

const someFunction = (name: string) => {
  return (value: number) => {
    console.log(name);
    console.log(value);
  };
}

const logNameHenryAndValue = someFunction("Henry");
logNameHenryAndValue(2);

Once logNameHenryAndValue(2) is run it should log

- "Henry"
- 2

This is called currying and can be used to compose functions: What is 'Currying'?

Jelle
  • 2,026
  • 1
  • 12
  • 17
1

The type of function is:

type ReturnFuction = (name:string) => ((value: number) => void);

This pattern called currying.

const name = "a";
const value = 5;
someFunction(name)(value);

Full example:

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);
Dennis Vash
  • 50,196
  • 9
  • 100
  • 118