Rya*_*edy 6 javascript ecmascript-6 flowtype
我正在项目中使用ES6和Flow类型检查器.
假设我有两个类型别名,仅根据期望的方法定义(如Java接口):
type Airplane = {
takeOff: (() => void);
land: (() => void);
};
type Car = {
drive: ((speed: number) => void);
};
Run Code Online (Sandbox Code Playgroud)
我如何定义一个类FlyingCar来向类型检查器演示它既是a Car又是Airplane?我正在使用ECMAScript 6类.
对于我怀疑它看起来像这样的类型:
type FlyingCar = (Airplane & Car);
Run Code Online (Sandbox Code Playgroud)
但是,我似乎无法调和我想要的类语法,因为它似乎与ES6的类语法联系在一起.
您不必证明它流动.Flow实现了结构类型系统,因此您只需在类中实现这两种类型.
这不是类型检查:
class FlyingCar {}
function flyInACar(car: Airplane & Car): void {
}
flyInACar(new FlyingCar());
Run Code Online (Sandbox Code Playgroud)
这样做:
class FlyingCar {
takeOff(): void {}
land(): void {}
drive(speed: number): void {}
}
function flyInACar(car: Airplane & Car): void {
}
flyInACar(new FlyingCar());
Run Code Online (Sandbox Code Playgroud)