Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flow interfaces versus object type aliases

What is the difference between these Flow type definitions?

interface Vehicle {
  start(): void,
  stop(): void
}
type Vehicle = {
  start(): void,
  stop(): void
};

As far as I can tell, they can be used in the same way.

like image 381
Mark Volkmann Avatar asked Mar 26 '17 01:03

Mark Volkmann


1 Answers

interfaces and types are similar, and the difference is mostly historical I believe. The recent changes to implement property variance also brought the behavior of type more in line with the behavior of interface. I believe the goal is to eventually make them identical and possibly even remove interface.

There may still be subtle difference but for most uses I don't think you will notice a difference.

One major difference is that if you want to use implements (e.g. class Foo implements Bar {...}) then Bar must be an interface -- not a type. However it's worth noting that marking a class as implementing an interface is not mandatory. Flow implements structural subtyping so if you have a class instance you can pass it to something that expects a compatible type even without explicitly indicating that the class implements a particular interface.

like image 106
Nat Mote Avatar answered Nov 17 '22 01:11

Nat Mote