From 27a6881ec4849c86ec21a5d0529ca5c93019c4ca Mon Sep 17 00:00:00 2001 From: Billy Schulze Date: Fri, 12 Jun 2020 15:04:22 +1000 Subject: [PATCH 1/2] fix withRouter types --- src/common/types.ts | 6 +++++- src/controllers/index.ts | 2 +- src/controllers/with-router/index.tsx | 18 +++++++++++------- src/index.ts | 1 + 4 files changed, 18 insertions(+), 9 deletions(-) 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..d396900e 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 = (unknownProps: any) => ( {( // @ts-ignore access private `history` store property @@ -36,7 +40,7 @@ export const withRouter =

>( { push, replace } ) => ( Date: Fri, 12 Jun 2020 15:19:42 +1000 Subject: [PATCH 2/2] remove "unknown" --- src/controllers/with-router/index.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/controllers/with-router/index.tsx b/src/controllers/with-router/index.tsx index d396900e..c6b7faa7 100644 --- a/src/controllers/with-router/index.tsx +++ b/src/controllers/with-router/index.tsx @@ -32,7 +32,7 @@ export const withRouter =

= {}>( ): ComponentType> => { const displayName = getWrappedComponentDisplayName(WrappedComponent); const Component = WrappedComponent; - const ComponentWithRouter = (unknownProps: any) => ( + const ComponentWithRouter = (props: any) => ( {( // @ts-ignore access private `history` store property @@ -40,7 +40,7 @@ export const withRouter =

= {}>( { push, replace } ) => (