1

What is the difference between these two interface declarations :

interface A {
  foo: (x: number) => number;
}

const a: A = {
  foo: (x: number) => (x + 1);
}
interface B {
  foo(x: number): number;
}

const b: B = {
  foo(x: number) {
    return x + 1;
  }
}

My guess is that foo in A is declared as an arrow function while foo in B is Function object (with its own this etc.), but I'm not sure. I couldn't find any info on this online, even after searching through the TypeScript doc. By the way, if you find anything online about this, I would be interested in knowing how did you find it (what did you type on Google to get your answer :p )

Ken White
  • 123,280
  • 14
  • 225
  • 444
souyahia
  • 200
  • 1
  • 9
  • 1
    They are interchangeable in your example – Matthieu Riegler Feb 25 '23 at 23:32
  • They are mostly the same, but when [`--strictFunctionTypes`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-6.html#strict-function-types) is enabled, then the function-valued property is checked more strictly than the method is; see [this playground link](https://tsplay.dev/N55adN) – jcalz Feb 26 '23 at 01:06
  • See the linked questions and answers for more information. To find these other questions I searched for "function property" and "method" and "TypeScript" and "difference". – jcalz Feb 26 '23 at 01:13

0 Answers0