Mixins with Flow type annotations

1.1k views Asked by At

I'm using ES6 and the Flow type checker in a project.

Suppose I've got two type aliases, defined only in terms of what methods are expected of them (like a Java interface):

type Airplane = {
    takeOff: (() => void);
    land: (() => void);
};

type Car = {
    drive: ((speed: number) => void);
};

How would I define a class FlyingCar to demonstrate to the type checker that it is both a Car and an Airplane? I'm using ECMAScript 6 classes.

For a type I suspect it would look something like:

type FlyingCar = (Airplane & Car);

I can't seem to reconcile what I want with the class syntax, though, since it seems to be tied into ES6's class syntax.

1

There are 1 answers

0
vkurchatkin On BEST ANSWER

You don't have to demonstrate it to flow. Flow implements structural type system, so you simply need to implement both type in your class.

This doesn't type check:

class FlyingCar {}

function flyInACar(car: Airplane & Car): void {

}

flyInACar(new FlyingCar());

this does:

class FlyingCar {
  takeOff(): void {}
  land(): void {}
  drive(speed: number): void {}
}


function flyInACar(car: Airplane & Car): void {

}

flyInACar(new FlyingCar());