Ernane Luis
Ernane Luis

Reputation: 361

How to write Typescript interface with double arrow functions

What is the syntax to write a typescript interface using double arrow function es6?

Example JS:

const myFunction => (param1) => (param2) => {
...code
}

Example: TS:

const myFunc = (param1: number) => (param2: number) => {
  return param1 + param2
};

this interface is incorrect

interface myInterface {
   myFunc: (param1: number) => (param2: number) => number
}

the error is: Parsing error: ';' expected so why? and what is the correct syntax?

Upvotes: 1

Views: 1133

Answers (1)

Pace
Pace

Reputation: 43797

I suspect the error is coming from your Javascript.

const myFunction => (param1) => (param2) => {
...code
}

That is not legal JS. Did you mean:

const myFunction = (param1) => (param2) => {
...code
}

The rest compiles just fine for me:

interface MyInterface {
   myFunc: (param1: number) => (param2: number) => number
}

const Foo: MyInterface = {

  myFunc: (param1: number) => (param2: number) => {
    return param1 + param2
  }

}

class FooClass implements MyInterface {

  myFunc(param1: number) {
    return (param2: number) => {
      return param1 + param2;
    }
  }

}

Upvotes: 1

Related Questions