Home:ALL Converter>Typescript: what is the type of a lambda?

Typescript: what is the type of a lambda?

Ask Time:2018-04-20T14:07:37         Author:Dave Branning

Json Formatter

In the Angular "Tour of Heroes" tutorial, an error handler method is defined which returns a lambda:

private handleError<T>(operation = 'operation', result?: T) {

  return (error: any): Observable<T> => {
    this.log(`${operation} failed: ${error.message}`);
    return of(result as T);
};

The handleError method has no return type and is therefore inferred. What would the return type be if we wanted to make it explicit? I've tried Function (lib.es2015.core.d.ts) but that doesn't work.

Adding the lambda itself as a return type works, but seems wrong:

  private handleError<T>(operation = 'operation', result?: T): (error: any) => Observable<T> {
          return (error: any): Observable<T> => {
          ...

What is the correct Typescript type for a lambda? In Java, handleError would return a java.util.function.Function. Is there anything similar in Typescript? Thank you.

Author:Dave Branning,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/49935062/typescript-what-is-the-type-of-a-lambda
yy