Flow provides two similar declarative structures that however serve different purposes:
Their main differences are the following:
- class types are nominal (structurally identical classes are not interchangeable)
- interface (as well as object) types are structural
- the main use case of class declarations is to allow client code using those classes
to type-check without having the implementation code available for typechecking.
Note, in that regard the following caveats (from the online Flow documentation):
Note that declarations are exclusively compile-time entities: they are transpiled away, so they have no effect at run time. In fact, declarations without backing implementations may cause typechecked uses to break at run time!
… and:
Note that it is entirely up to the programmer to ensure that declared definitions actually exist, and have the correct types.
- as a consequence of the above I have found interfaces to be more usefull (if not the only way) for typing the contract / behaviour of a base class and supporting polymorphism for the derived classes
- yet, it appears that only classes can be parameterized (polymorphic classes) and this is a feature I've used (see this tree class) whereas I am not sure whether interfaces can also be parameterized
The following playground projects of mine demonstrate the differences: