Function property vs method

前端 未结 4 2031
春和景丽
春和景丽 2020-11-30 11:00

What practical differences are there between defining an interface method:

interface Foo {
    bar(): void;
}

and defining a property with

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 11:24

    If these are the only declarations, these are identical.

    The only difference is that you can augment the first form in a second declaration to add new signatures:

    // Somewhere
    interface Foo {
      bar(): void;
    }
    
    // Somewhere else
    interface Foo {
      bar(s: number): void;
    }
    
    // Elsewhere
    let x: Foo = ...
    x.bar(32); // OK
    

提交回复
热议问题