为react-router-relay创建新的类型定义

时间:2016-11-06 14:13:19

标签: typescript react-router react-router-relay

我想为react-router-relay创建Typescript定义,但react-router-relay的扩展性质让我很难过。 react-router正在向路由器组件公开一些属性,还包括一个render属性。 react-router-relay使用此render属性为Router提供中间件。在此路由器之后还接受可以给出中继的环境属性。

如何告诉typescript在我的模块react-router-relay中我扩展了模块react-router的定义?

我试过跟随,但后来我必须复制react-router的每个组件:

declare module "react-router-relay" {
    import {Middleware} from "react-router/lib/applyRouterMiddleware";
    import Router from "react-router/lib/Router";
    import self from "react-router/lib/Route";

    const RelayMiddleware:Middleware;
    export default RelayMiddleware;

    export interface RelayRouter extends React.ComponentClass<RelayRouterProps> { }

    interface RelayRouterProps extends Router.RouterProps {
        render?(...middlewares: any[]): any
        environment?: any
    }

    namespace Route {
        import RouteProps = self.RouteProps;
        export interface RelayRouterProps extends RouteProps {
            name?: string;
            queries?: any;
            render(props: any): React.Component<any, any>
        }
    }
}

是否也可以直接扩展react-router的RouterProps和RouteProps?

编辑:路由器中继应用如下:

import useRelay from 'react-router-relay';
...
<Router
    history={browserHistory}
    routes={routes}
    render={applyRouterMiddleware(useRelay)}
    environment={Relay.Store}
/>

0 个答案:

没有答案