带有流类型注释的混合
Mixins with Flow type annotations
我在项目中使用 ES6 和 Flow 类型检查器。
假设我有两个 type aliases,仅根据对它们的预期方法(如 Java 接口)进行定义:
type Airplane = {
takeOff: (() => void);
land: (() => void);
};
type Car = {
drive: ((speed: number) => void);
};
我如何定义 class FlyingCar
来向类型检查器证明它既是 Car
又是 Airplane
?我正在使用 ECMAScript 6 classes.
对于一个类型,我怀疑它看起来像:
type FlyingCar = (Airplane & Car);
不过,我似乎无法将我想要的内容与 the class syntax 相协调,因为它似乎与 ES6 的 class 语法相关联。
您不必证明它可以流动。 Flow 实现结构类型系统,因此您只需在 class.
中实现这两种类型
这不是类型检查:
class FlyingCar {}
function flyInACar(car: Airplane & Car): void {
}
flyInACar(new FlyingCar());
这样做:
class FlyingCar {
takeOff(): void {}
land(): void {}
drive(speed: number): void {}
}
function flyInACar(car: Airplane & Car): void {
}
flyInACar(new FlyingCar());
我在项目中使用 ES6 和 Flow 类型检查器。
假设我有两个 type aliases,仅根据对它们的预期方法(如 Java 接口)进行定义:
type Airplane = {
takeOff: (() => void);
land: (() => void);
};
type Car = {
drive: ((speed: number) => void);
};
我如何定义 class FlyingCar
来向类型检查器证明它既是 Car
又是 Airplane
?我正在使用 ECMAScript 6 classes.
对于一个类型,我怀疑它看起来像:
type FlyingCar = (Airplane & Car);
不过,我似乎无法将我想要的内容与 the class syntax 相协调,因为它似乎与 ES6 的 class 语法相关联。
您不必证明它可以流动。 Flow 实现结构类型系统,因此您只需在 class.
中实现这两种类型这不是类型检查:
class FlyingCar {}
function flyInACar(car: Airplane & Car): void {
}
flyInACar(new FlyingCar());
这样做:
class FlyingCar {
takeOff(): void {}
land(): void {}
drive(speed: number): void {}
}
function flyInACar(car: Airplane & Car): void {
}
flyInACar(new FlyingCar());