默认的react-router是这样使用的:
import * as React from 'react';
import { Router, Route, hashHistory } from 'react-router';
const routing = (
<Router history={hashHistory}>
<Route path="/login" component={Login}/>
</Router>
};
当我包含“react-router-relay”库时,它为路由器添加了功能。也就是说,它向 Router 组件添加了 2 个属性(渲染和环境):
import * as React from 'react';
import * as Relay from 'react-relay';
import * as useRelay from 'react-router-relay';
import { Router, Route, hashHistory, applyRouterMiddleware } from 'react-router';
const routing = (
<Router history={hashHistory} render={applyRouterMiddleware(useRelay)} environment={Relay.Store}>
<Route path="/login" component={Login}/>
</Router>
};
我将如何增加反应路由器的类型?
我尝试了很多方法,最新的是:
import { Router } from 'react-router';
declare module 'react-router' {
namespace Router {
export interface RouterProps {
environment?: any
}
}
}
因为我需要扩展命名空间“Router”和其下的接口“RouteProps”。
链接到 React 路由器类型:https://www.npmjs.com/package/@types/react-router https://www.npmjs.com/package/@types/react-router
React-router-relay 库本身没有任何类型。
我找到的有关此主题的所有信息:
- https://github.com/Microsoft/TypeScript/issues/11034 https://github.com/Microsoft/TypeScript/issues/11034
- https://github.com/typings/typings/issues/611 https://github.com/typings/typings/issues/611
so the problem seems to be that react typings don't ever export the namespaces
so it becomes impossible to augment them