別のモジュールの宣言を使用するモジュールインターフェイス宣言を作成しようとしていますが、うまく動作しません。ここに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
Iは4つのエラー(A、B、Cを期待Dはindex.js
)、Aのみを取得すると、Foo
がlib/bar.js
に正しくインポートされないため、Foo
はワイルドカードのようになります。
モジュールのタイプを別のインタフェース宣言に正しくインポートする方法はありますか?何か不足していますか?どんな助けもありがとうございます。
[EDIT]
私はそれをインポートせず、flow-typed使用React$Component
、which is declared globallyにいくつかの宣言を参照してください。しかし、私はComponent
in react
moduleのようなモジュールで型を使いたいと思います。
あなたはそうです。しかし、私は実際に公式宣言をしていない第三者図書館の宣言ファイルを作成しようとしています。その例はMCVEです。 http://stackoverflow.com/help/mcve 'foo.js'と' bar.js'にはサードパーティのライブラリの例であるため、 '/ * @flow * /'はありません。 –