diff --git a/src/common/types.ts b/src/common/types.ts index 858cade2..f2643a18 100644 --- a/src/common/types.ts +++ b/src/common/types.ts @@ -110,12 +110,16 @@ export type RouterStoreContext = { location: Location; }; +type RouteComponentProps = Partial & { + [k: string]: any; +}; + export type Route = { path: string; exact?: boolean; /** The component to render on match, typed explicitly */ - component: ComponentType; + component: ComponentType | ComponentType; /** If present, must return true to include the route. */ enabled?: () => boolean; diff --git a/src/controllers/index.ts b/src/controllers/index.ts index 08d2143a..64ed8a08 100644 --- a/src/controllers/index.ts +++ b/src/controllers/index.ts @@ -4,7 +4,7 @@ export { MemoryRouter } from './memory-router'; export { StaticRouter } from './static-router'; export { RouterActions } from './router-actions'; export { Redirect } from './redirect'; -export { withRouter } from './with-router'; +export { withRouter, WithRouterProps } from './with-router'; export { useResource, useRouter, useRouterActions } from './hooks'; export { useResourceStoreContext } from './resource-store'; export { createResource } from './resource-utils'; diff --git a/src/controllers/with-router/index.tsx b/src/controllers/with-router/index.tsx index 7311875a..c6b7faa7 100644 --- a/src/controllers/with-router/index.tsx +++ b/src/controllers/with-router/index.tsx @@ -1,10 +1,14 @@ import React, { ComponentType } from 'react'; import { BrowserHistory, RouteContext } from '../../common/types'; +import { RouterActionPush, RouterActionReplace } from '../router-store/types'; import { RouterSubscriber } from '../subscribers/route'; -// TODO -type WithRouter = RouteContext & { history: BrowserHistory }; +export type WithRouterProps = RouteContext & { + history: BrowserHistory; + push: RouterActionPush; + replace: RouterActionReplace; +}; const getWrappedComponentDisplayName = ( component: ComponentType @@ -23,12 +27,12 @@ const getWrappedComponentDisplayName = ( return `withRouter(${componentDisplayName})`; }; -export const withRouter =

>( +export const withRouter =

= {}>( WrappedComponent: ComponentType

-) => { +): ComponentType> => { const displayName = getWrappedComponentDisplayName(WrappedComponent); - const Component = WrappedComponent as ComponentType; - const ComponentWithRouter = (props: P) => ( + const Component = WrappedComponent; + const ComponentWithRouter = (props: any) => ( {( // @ts-ignore access private `history` store property diff --git a/src/index.ts b/src/index.ts index f775a43f..ec383f21 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,6 +7,7 @@ export { Redirect, RouterActions, withRouter, + WithRouterProps, ResourceSubscriber, useResource, useRouter,