Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix withRouter types #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,16 @@ export type RouterStoreContext = {
location: Location;
};

type RouteComponentProps = Partial<RouteContext> & {
[k: string]: any;
};

export type Route = {
path: string;
exact?: boolean;

/** The component to render on match, typed explicitly */
component: ComponentType<RouteContext>;
component: ComponentType<RouteComponentProps> | ComponentType<any>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this just be made into an optional generic like in the withRouter case? This should only need to be supported for consumers that do not use the exported RouteComponent component, and I would avoid the use of any here as a result. The default prop type just just be RouteComponentProps for those using the route component.


/** If present, must return true to include the route. */
enabled?: () => boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
18 changes: 11 additions & 7 deletions src/controllers/with-router/index.tsx
Original file line number Diff line number Diff line change
@@ -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<any>
Expand All @@ -23,20 +27,20 @@ const getWrappedComponentDisplayName = (
return `withRouter(${componentDisplayName})`;
};

export const withRouter = <P extends Record<string, any>>(
export const withRouter = <P extends Record<string, any> = {}>(
WrappedComponent: ComponentType<P>
) => {
): ComponentType<Omit<P, keyof WithRouterProps>> => {
const displayName = getWrappedComponentDisplayName(WrappedComponent);
const Component = WrappedComponent as ComponentType<WithRouter & P>;
const ComponentWithRouter = (props: P) => (
const Component = WrappedComponent;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/nit May as well just renamed the WrappedComponent argument to Component?

const ComponentWithRouter = (unknownProps: any) => (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is "unknown" necessary?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no it's not

<RouterSubscriber>
{(
// @ts-ignore access private `history` store property
{ route, location, query, match, action, history },
{ push, replace }
) => (
<Component
{...props}
{...unknownProps}
route={route}
location={location}
query={query}
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export {
Redirect,
RouterActions,
withRouter,
WithRouterProps,
ResourceSubscriber,
useResource,
useRouter,
Expand Down