Search code examples
typescriptecmascript-6arrow-functions

TypeScript arrow functions double return


I have been reading some React code written in TS and I've come across this code:

const loggedInRequired = () => (
    toState: State,
    fromState: State,
    // tslint:disable-next-line:no-any
    done: any
) => {
    // userIsLoggedIn can be whatever you need it to be
    if (isAuthenticated()) {
      return true;
    } else {
      // redirect to signin page if the user isn't logged in
      done({ redirect: { name: Routes.Login } });
      return false;
    }
  };

I want to know what this specific part means:

() => (someParam: someType, someOtherParam: someType) => { someCodeHere }

what does the part in between second group of parenthesis represent ? It looks like an interface but I can't see the logic behind it, done is obviously a function because we call it later in the code, but I can't understand the general meaning of this code.

EDIT

It's just me being retarded.


Solution

  • It's a function (that starts at () =>) that returns another function (the other function starting at (toState: State, fromState: State,done: any ) =>).

    When you call loggedInRequired the result will be a function

    const loggedInRequiredResult = loggedInRequired (); // will be a (toState: any, fromState: any, done: any) => boolean
    const finalResult = loggedInRequiredResult (null, null, null) // will be a boolean