【发布时间】:2016-11-08 11:25:14
【问题描述】:
我有以下使用 Flow 的代码:
// @flow
'use strict';
import assert from 'assert';
declare interface IPoint {
x: number;
y: number;
distanceTo(other: IPoint): number;
}
class Point {
x: number;
y: number;
distanceTo(a: IPoint): number {
return distance(this, a);
}
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}
function distance(p1: IPoint, p2: IPoint): number {
function sq(x: number): number {
return x*x;
}
return Math.sqrt( sq(p2.x-p1.x)+sq(p2.y-p1.y) );
}
assert(distance ( new Point(0,0), new Point(3,4))===5);
// distance ( new Point(3,3), 3); // Flow complains, as expected
assert((new Point(0,1)).distanceTo(new Point(3,5))===5);
// (new Point(0,1)).distanceTo(3); // Flow complains as expected
运行 npm run flow 不会产生预期的抱怨,而被注释掉的行会产生警告(再次,如预期的那样)。
所以世界上一切都很好,除了我不知道如何在类Point 被定义为“实现”接口IPoint 的地方明确说明它。有没有办法这样做还是不是惯用的?
【问题讨论】:
标签: javascript node.js flowtype