Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference of TypeScript function declaration in interfaces

What is the difference between these two declarations of functions in TypeScript Interfaces?

interface IExample {
  myFunction(str: string): void;
}

and

interface IExample {
  myFunction: (str: string) => void;
}
like image 369
Anton Selin Avatar asked Nov 24 '25 20:11

Anton Selin


1 Answers

These declarations are completely equivalent.

The only relevant difference here is that the second form can't be used for function overloads:

// OK
interface Example {
    myFunction(s: string): void;
    myFunction(s: number): void;
}

// Not OK
interface Example {
    myFunction: (s: string) => void;
    myFunction: (s: number) => void;
}
like image 62
Ryan Cavanaugh Avatar answered Nov 26 '25 08:11

Ryan Cavanaugh