2016-09-18 5 views
0

私は長い間何百行ものコードを扱っていますが、私の問題は次の例に煮詰めました。私の質問は、Typescriptコンパイラが1つのケースで失敗する理由ですが、基礎となるクラスが使用されたときに成功します。typescriptクラスを拡張すると、基本クラスで動作するコンパイル時に反応ファクトリメソッドが失敗するのはなぜですか?

次のコードはうまくコンパイル:

import * as React from "react"; 

interface ComponentProps { 
} 

class Component<P extends ComponentProps> extends React.Component<P, any> { 
} 

interface RootComponentProps extends ComponentProps { 
    viewManager?: ViewManager; 
} 

class RootComponent<P extends RootComponentProps> extends Component<P> { 

    static childContextTypes = { 
     viewManager: React.PropTypes.object 
    } 

    private _viewManager: ViewManager; 

    getChildContext(): RootComponentProps { 
     return { viewManager: this._viewManager } 
    }; 
} 

class ViewManager { 
    constructor(managedElement: HTMLElement, rootComponent: typeof Component) { 
     const rootFactory = React.createFactory(rootComponent); 
    } 
} 

私は1つの変更を行う場合は、しかし、ViewManager、コンパイラ(およびVS2015インテリセンス)のコンストラクタで「typeof演算RootComponent」を「typeof演算コンポーネント」を変更すること文句を言います:

import * as React from "react"; 

interface ComponentProps { 
} 

class Component<P extends ComponentProps> extends React.Component<P, any> { 
} 

interface RootComponentProps extends ComponentProps { 
    viewManager?: ViewManager; 
} 

class RootComponent<P extends RootComponentProps> extends Component<P> { 

    static childContextTypes = { 
     viewManager: React.PropTypes.object 
    } 

    private _viewManager: ViewManager; 

    getChildContext(): RootComponentProps { 
     return { viewManager: this._viewManager } 
    }; 
} 

class ViewManager { 
    constructor(managedElement: HTMLElement, rootComponent: typeof RootComponent) { 
     const rootFactory = React.createFactory(rootComponent); 
    } 
} 

エラーがReact.createFactoryメソッドの引数である、と述べている:

エラーTS2345: 'typeof RootComponent'型の引数が 'ComponentClass'型のパラメータに割り当てられない<any> | StatelessComponent <任意> 'です。

タイプ 'typeof演算RootComponent'provides署名のための一致なし'(小道具:?いずれかを、すなわち、React.createFactoryに引数の上にマウスを移動するとき

VS 2015のIntelliSenseは、もう少し情報を提供しますコンテキスト:?任意の):ReactElement <任意の>「

このすべてのもう一つの奇妙な癖....私はRootComponentで静的 『childContextTypes』を削除またはコメントアウトした場合、それはすべて罰金コンパイルしますが、私は自分の仕事でそれを実行するongerアプリケーションでは、childContextTypesの不足に関するランタイムの苦情を受けます。

私はこれが本当に魔法ではないことを知っていますが、現時点では私には黒魔法のように思えますし、明確な洞察が最も高く評価されます。

+0

少し実験したところ、これはバグだと確信しています。私はTSレポに関する問題を提出する。 – Mosho

+0

エラーが表示されない場合は、このコードが問題を引き起こしている可能性がありますか? –

+0

私はこれを得ていると確信しています。しかし、おそらくあなたの経験は入力ファイルの問題を指しているでしょう。私の反応入力ファイルはGithubのDefinitelyTypedからですが、React自体と比較して非常に古くなっています。私のおかげで、入力ファイルはReact v0.14であると主張しています。おそらく、あなたの入力ファイルはもっと最新のものです。あなたはそれをどこから得たか分かち合うことができますか? –

答えて

0

質問のコメントを確認した後、私は入力の疑いがあり始めました。そして私は以下を見つけた。helpful gotcha

キーポイントは次のように私のchildContextTypesに明示的な型注釈を追加することです:

static childContextTypes: React.ValidationMap<any> = { 
    viewManager: React.PropTypes.object 
} 

これはかなり鈍角コンパイルエラーを解決します。私はまだ動作していないし、すべてのコードが動作するかどうかをテストしたが、これは確かにこの状況のた​​めの実行可能な回避策であり、リンクによると、これはTypescript 2.0で修正する必要があります。

関連する問題