2016-06-27 10 views
0

別のモジュールの宣言を使用するモジュールインターフェイス宣言を作成しようとしていますが、うまく動作しません。ここに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のみを取得すると、Foolib/bar.jsに正しくインポートされないため、Fooはワイルドカードのようになります。

モジュールのタイプを別のインタフェース宣言に正しくインポートする方法はありますか?何か不足していますか?どんな助けもありがとうございます。

[EDIT]

私はそれをインポートせず、flow-typed使用React$Componentwhich is declared globallyにいくつかの宣言を参照してください。しかし、私はComponent in react moduleのようなモジュールで型を使い​​たいと思います。

答えて

1

Flowはソースを直接使用するため、コンポーネントの宣言ファイルを作成するとは思われません。あるタイプへの参照が必要な場合は、単にimportクラスを使用します。

現在の例では、ファイルの先頭に/* @flow */bar.jsfoo.js)が表示されています。

+0

あなたはそうです。しかし、私は実際に公式宣言をしていない第三者図書館の宣言ファイルを作成しようとしています。その例はMCVEです。 http://stackoverflow.com/help/mcve 'foo.js'と' bar.js'にはサードパーティのライブラリの例であるため、 '/ * @flow * /'はありません。 –

0

残念ながら、フローはフロー/ libの種類を上書き/拡張をサポートしていません:

私がやり始めて何

https://github.com/facebook/flow/issues/396

は次のとおりです。フローから

  1. flow-typed install [email protected]
  2. 移動express_v4.x.x.js -typed/npm /をflow-typedにする/(flow-typed/npm /の外にあるので、将来のフロータイプのインストールでは上書きされず、フロータイプ/フローの内部で自動的にグローバル文)右下の
  3. declare class express$Request...(それを見つけるのは簡単ですので、それはそれはdeclare module...内部で使われている場所より上だが、私が入れ:

    declare class express$Request extends express$Request { user: any; isAuthenticated(): boolean; session: { loginForwardUrl: ?string; }; }

私はこれを行う代わりに私を入れて元のクラスのカスタム小道具を使用して、どの小道具がカスタムかを簡単に確認できます。

関連する問題