1

在一个项目上工作并看到开发人员在 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 组件中有对接口或类的引用。想要开始建立一些约定,但想要在这种情况下的正确做法方面获得一些帮助?

4

1 回答 1

3

It seems pointless if there is no implementation in the class.

I agree. No need in that case. But there are valid cases

Dependency inject

Something like : https://github.com/inversify/InversifyJS

Conforming to external Apis

Someone asks for IFoo. You want to use a class in your code base for IFoo. Have a class extend it so you know that the class always follows that external IFoo.

于 2016-09-08T23:58:25.960 回答