react-router-dom TypeScript TS2322: тип "индекс typeof" не присваивается типу
Im получает довольно много той же ошибки TypeScritp в моем основном компоненте React с помощью react-router-dom. Все дочерние компоненты возвращают ошибки:
error TS2322: Type 'typeof NotFound' is not assignable to type 'StatelessComponent<void | RouteComponentProps<any>> | ComponentClass<void | RouteComponentProps<a...'.
Type 'typeof NotFound' is not assignable to type 'ComponentClass<void | RouteComponentProps<any>>'.
Types of parameters 'props' and 'props' are incompatible.
Type 'void | RouteComponentProps<any> | undefined' is not assignable to type '{} | undefined'.
Type 'void' is not assignable to type '{} | undefined'.
error TS2322: Type 'typeof Index' is not assignable to type 'StatelessComponent<void | RouteComponentProps<any>> | ComponentClass<void | RouteComponentProps<a...'.
Type 'typeof Index' is not assignable to type 'ComponentClass<void | RouteComponentProps<any>>'.
Type 'Index' is not assignable to type 'Component<void | RouteComponentProps<any>, ComponentState>'.
Types of property 'props' are incompatible.
Type 'Readonly<{ children?: ReactNode; }> & Readonly<Props>' is not assignable to type '(Readonly<{ children?: ReactNode; }> & void) | (Readonly<{ children?: ReactNode; }> & Readonly<Ro...'.
Type 'Readonly<{ children?: ReactNode; }> & Readonly<Props>' is not assignable to type 'Readonly<{ children?: ReactNode; }> & Readonly<RouteComponentProps<any>>'.
Type 'Readonly<{ children?: ReactNode; }> & Readonly<Props>' is not assignable to type 'Readonly<RouteComponentProps<any>>'.
Property 'match' is missing in type 'Readonly<{ children?: ReactNode; }> & Readonly<Props>'.
Мой Главный.компонент tsx выглядит так (все остальные компоненты.tsx, похоже, компилируется без проблем):
import * as React from 'react';
import { BrowserRouter as Router, Route, Switch} from 'react-router-dom';
import Index from './Index';
import Explore from './Explore';
import NotFound from './NotFound';
class Main extends React.Component<{},{}> {
render () {
return (
<Router>
<Switch>
<Route path="/" exact={true} component={Index} />
<Route path="/explore/things/:id" component={Explore} />
<Route component={NotFound} />
</Switch>
</Router>
)
}
}
export default Main;
может кто-нибудь дать некоторые подсказки?
3 ответов
это исправляет ошибку компиляции (но создает нарушение tslint)
<Route path="/" exact={true} component={Index as any} />
<Route path="/explore/things/:id" component={Explore as any} />
Я делаю это так
import { RouteComponentProps } from 'react-router-dom';
class Index extends React.Component<RouteComponentProps<any>, {}> {...}
или
import { RouteComponentProps } from 'react-router-dom';
interface Props extends RouteComponentProps<any> {...}
class Index extends React.Component<Props, {}> {...}
Это можно решить, вызвав компонент маршрута с помощью функции стрелки и пройдя в props: any
С:
<Route
path="/explore/things/:id"
component={(props: any)=>
<Explore {...props} user_role={this.state.user_role} />
} />
этот метод хорош, потому что он позволяет передавать дополнительные реквизиты, user_role
, к дочернему компоненту маршрутизатора React.
поочередно, меняя @types/react-router-dom
определения, казалось, избавились от ошибки:
interface RouteProps {
...
//component?: React.SFC<RouteComponentProps<any> | void> | React.ComponentClass<RouteComponentProps<any> | void>;
component?: any;
но это не кажется отличным решением.