TypeScript 2.6 introduced strictFunctionTypes
アップグレード後、私はReduxとReact Routerがうまくやってくれないようです。したがって、特定のルートに表示されるコンポーネントと、Reduxからのプロップを受け取るコンポーネントの両方のタイプを最適に定義する方法が不思議です。タイプ・スクリプトの `strictFunctionTypes`をReact RouterとReduxで照合する方法は?
以下は、私のアプリケーションの設定方法です。
<Route path="/some/path/with/:parameter" component={ConnectedComponent}/>
その後、私は表示させたいコンポーネントはReduxの両方から小道具を受け取り、私は次の型シグネチャで捕獲しようとしたルータを、リアクト:このようなルートがあります
class BareComponent extends React.Component<ReduxProps & RouteProps, ArbitraryState>
interface StateProps { storeProperty: string; }
interface DispatchProps { dispatchCallback:() => void; }
type ReduxProps = StateProps & DispatchProps;
...とやや次のようにルータの小道具:
Reduxの小道具は、次のように定義されています...
const ConnectedComponent = connect<StateProps, DispatchProps, RouteProps, ArbitraryStateInterface>(mapStateToProps)(BareComponent);
あいにくと、(RouteComponentProps
はreact-router-dom
からインポートされた場合)次のように
interface RouteParams { parameter: string }
type RouteProps = RouteComponentProps<RouteParams>;
(上記の最初のサンプルコードでRoute
を介して)ルータに渡されるコンポーネントが作成されstrictFunctionTypes
、タイプルータは、ConnectedComponent
がReact Routerが予想しているタイプに割り当てられないと不平を言っています:
TS2322: Type '{ path: "/some/path/with/:parameter"; component: ComponentClass<Pick<StatePr...' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Route> & Readonly<{ children?: ReactNode; }> & Rea...'.
Type '{ path: "/some/path/with/:parameter"; component: ComponentClass<Pick<StatePr...' is not assignable to type 'Readonly<RouteProps>'.
Types of property 'component' are incompatible.
Type 'ComponentClass<Pick<StateProps & DispatchProps & RouteC...' is not assignable to type 'StatelessComponent<RouteComponentProps<any> | undefined> | ComponentClass<RouteComponentProps<any...'.
Type 'ComponentClass<Pick<StateProps & DispatchProps & RouteC...' is not assignable to type 'ComponentClass<RouteComponentProps<any> | undefined>'.
Types of parameters 'props' and 'props' are incompatible.
Type 'RouteComponentProps<any> | undefined' is not assignable to type 'Pick<StateProps & DispatchProps & RouteComponentProps<R...'.
Type 'undefined' is not assignable to type 'Pick<StateProps & DispatchProps & RouteComponentProps<R...'.
Type 'undefined' is not assignable to type 'Pick<StateProps & DispatchProps & RouteComponentProps<R...'.
上記の型を定義する方法は、型の使用方法に沿っていませんか? any
の使用に頼らずにstrictFunctionTypes
を有効にする手段はありますか?
ええと、私はもう少し 'withRouter'に分割しましたが、私はそれが私の問題を解決するとは思いませんか?私が見ている限り、 'withRouter'はルータの小道具を接続されたコンポーネントに渡しますが、' 'はそれと同じことをしません。 ''(またはその型定義)が 'withRouter'でラップされたコンポーネントを期待するのはなぜですか? –
Vincent