【发布时间】:2016-11-04 00:08:15
【问题描述】:
我在b.js 文件中定义了一些类(在本例中名为B),它是在b.js.flow 文件中定义的声明。我假设 flowtype 将类与它的声明相关联,但不幸的是,当我尝试使用一些接受 B 类实例的函数时,它的方法 flowtype 引发了以下错误:
b.js:9
9: return this.a.foo(this)
^^^^^^^^^^^^^^^^ call of method `foo`
5: export default class B {
^ B. This type is incompatible with
6: foo(b: B): null;
^ B. See: a.js.flow:6
我猜这是由treating classes as nominal types 引起的。有什么方法可以将类实现与其声明相关联以防止出现此错误?
有我用于测试的完整文件内容:
a.js:
// @flow
import type B from './b'
export default class A {
foo(b: B) {
return null
}
}
a.js.flow:
// @flow
import type B from './b'
declare export default class A {
foo(b: B): null;
}
b.js:
// @flow
import A from './a'
export default class B {
a: A;
bar() {
return this.a.foo(this)
}
}
b.js.flow:
// @flow
import type A from './a'
declare export default class B {
a: A;
bar(): null;
}
【问题讨论】:
标签: javascript flowtype