什么时候一个类应该实现一个接口



在一个项目中,看到开发人员在typescript中做以下事情。

    export class Ledger implements ILedger {
      LedgerID: number;
      CashAmmount: number;
      Units: number;
      public static someFunction {
        // an ajax call for example to a controller
      }
    }
    export interface ILedger {
       LedgerID: number;
       CashAmmount: number;
       Units: number;
     }

想知道这是不是正确的做事方式。如果在类中没有实现,似乎毫无意义。然后在我们的React组件中有对接口或类的引用。想要开始建立一些惯例,但在这种情况下需要一些关于正确实践的帮助?

如果在类中没有实现,则似乎毫无意义。

我同意。那样就没必要了。但也有有效的情况

<标题>依赖注入h1> 如:https://github.com/inversify/InversifyJS

符合外部api

有人要IFoo。您希望在代码库中为IFoo使用一个类。让一个类扩展它,这样你就知道这个类总是遵循这个外部IFoo。

相关内容

最新更新