using TypeScript:
public subscribe:(subscribeFunction:(state)=>void)=>()=>void;
It's a subscribe method that gets function as an argument, of type function, and that function given, will receive a state argument when called, that argumented function will not return anything (i.e.: void) ... and I am lost on the last ()=>()=>void
.
Do I understand this right?
ES6 version of TypeScript provides an arrow function which is the shorthand syntax for defining the anonymous function, i.e., for function expressions. It omits the function keyword. We can call it fat arrow (because -> is a thin arrow and => is a "fat" arrow).
Arrow function is one of the features introduced in the ES6 version of JavaScript. It allows you to create functions in a cleaner way compared to regular functions. For example, This function // function expression let x = function(x, y) { return x * y; }
In a type position, => defines a function type where the arguments are to the left of the => and the return type is on the right. So callback: (result: string) => any means " callback is a parameter whose type is a function.
Set the return type of an arrow function in TypeScript # You can set the return type of an arrow function in TypeScript right after its parameters, e.g. const greet = (name: string): string => {} . Once a function's return type is set, the type checker alerts us if the function returns a value of a different type.
It's for a public property called subscribe
that has a type of (subscribeFunction: (state) => void) => () => void;
:
// scope
public
// name
subscribe:
// type (function)
// parameters
(
// parameter name
subscribeFunction:
// parameter type (function)
(state) => void
) =>
// return type (function)
() => void;
Here's an example that compiles:
class MyClass {
public subscribe: (subscribeFunction: (state) => void) => () => void;
}
let myInstance = new MyClass();
myInstance.subscribe = (subscribeFunction: (state) => void) => {
console.log("statements might go here");
return () => {
subscribeFunction(1 /* state */);
console.log("nothing returned by this inner function");
};
};
// Example use
// outputs "statements might go here"
let innerFunction = myInstance.subscribe((state) => console.log(state));
// outputs 1 and "nothing returned by this inner function"
innerFunction();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With