Search code examples
typescriptlambdasymbols

What do the two symbols => in a row mean


I know lambdas and function like type, but what is this:

displayFunc: (string) => string = x => x;

Two symbols "=>" confuse me. What is the result?


Solution

  • One is type the other one is implementation.

    type MyFunctionType = (string) => string
    
    const displayFunc: MyFunctionType = x => x;
    
    /// OR
    
    const displayFunc: MyFunctionType = function(x: string): string {
      return x;
    };
    

    (string) => string means a function type that takes string and returns string;

    x => x is an identity function, meaning it returns the same thing it was passed in.