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.
interface
s and type
s 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With