Flowtype class 声明与其实现不兼容

Flowtype class declaration incompatible with it's implementation

我在 b.js 文件中定义了一些 class(在这个例子中它被命名为 B),它的声明在 b.js.flow 文件中定义。我假设 flowtype 将 classes 与其声明相关联,但不幸的是,当我尝试使用某些函数接受 class 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 引起的。有什么方法可以将 class 实现与其声明相关联以防止出现此错误吗?

我用于测试的文件的完整内容:

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;
}
如果 .js 文件也由 Flow 检查,

.js.flow 文件并不意味着与它们的 .js 对应文件并排放置。目的是使人们能够通过以下方式在 npm 上发布流类型声明:(a) 转译原始源文件,这样人们就可以 运行 它们而无需自己的转译步骤,以及 (b) 添加 .flow 扩展名原始源文件,它们仍然有类型,因此 Flow 可以进行类型检查。 flow-typed 出于各种原因优于此方法,但这是一个单独的问题。

.js.flow 文件 而不是 意味着用作 .h 风格的声明文件。最有可能的是,最好的解决方案是简单地删除您的 .js.flow 文件。当我在没有 .js.flow 文件的情况下测试你的示例时,它的类型检查很好。