Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixins with Flow type annotations

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.

like image 558
Ryan Kennedy Avatar asked Jun 20 '15 22:06

Ryan Kennedy


1 Answers

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());
like image 163
vkurchatkin Avatar answered Sep 21 '22 02:09

vkurchatkin