【发布时间】:2016-06-27 05:49:50
【问题描述】:
我正在尝试创建一个使用另一个模块声明的模块接口声明,但效果不佳。这是一个 MCVE。我知道我可以将类型注释直接放在本地模块上,但我试图描述我对第三方模块的体验。
.flowconfig:
[ignore]
[include]
[libs]
lib
[options]
[version]
^0.27.0
foo.js:
export class Foo {
foo() {
return 'foo';
}
}
bar.js:
import { Foo } from './foo';
export class Bar extends Foo {
bar() {
return 'bar';
}
makeFoo() {
return new Foo();
}
}
lib/foo.js:
declare module './foo' {
declare class Foo {
foo(): string;
}
}
lib/bar.js:
import { Foo } from './foo';
// Same result if:
// import { Foo } from '../foo';
declare module './bar' {
declare class Bar extends Foo {
bar(): string;
makeFoo(): Foo;
}
}
index.js:
/* @flow */
import { Foo } from './foo';
import { Bar } from './bar';
const bar = new Bar();
const b: number = bar.bar(); // A. wrong const type
const bf: number = bar.foo(); // B. wrong const type
bar.typo(); // C. no such method
const foo = bar.makeFoo();
foo.foo();
foo.bar(); // D. no such method
而flow的结果是:
index.js:6
6: const b: number = bar.bar(); // wrong const type
^^^^^^^^^ call of method `bar`
6: const b: number = bar.bar(); // wrong const type
^^^^^^^^^ string. This type is incompatible with
6: const b: number = bar.bar(); // wrong const type
^^^^^^ number
Found 1 error
我预计会出现 4 个错误(index.js 中的 A、B、C 和 D),但只得到 A。似乎我无法在 lib/bar.js 中正确导入 Foo,因此 Foo 变得类似于通配符。
有没有办法将模块的类型正确地导入另一个接口声明?我错过了什么吗?任何帮助表示赞赏。
[编辑]
我在flow-typed 中看到一些声明使用React$Component、which is declared globally,但没有导入它。但我想在 Component in react module 这样的模块中使用类型。
【问题讨论】:
标签: flowtype