Skip to content

Commit

Permalink
fix: Define type for useRouter (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
taion committed Apr 12, 2019
1 parent 599fd20 commit 3093fb0
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
18 changes: 18 additions & 0 deletions types/example-app/ComponentUsingRouter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import useRouter from 'found/lib/useRouter';
import * as React from 'react';

interface Props {
foo?: boolean;
}

function ComponentUsingRouter(_props: Props) {
const { match, router } = useRouter();

return (
<div>
{match.location.pathname}, {router}
</div>
);
}

export { ComponentUsingRouter };
24 changes: 10 additions & 14 deletions types/example-app/ComponentWithRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import { WithRouterProps } from 'found';
import { Match, Router } from 'found';
import withRouter from 'found/lib/withRouter';
import * as React from 'react';

interface Props extends WithRouterProps {
interface Props {
foo?: boolean;
match: Match;
router: Router;
}

class ComponentWithRouter extends React.Component<Props> {
render() {
const {
match: { location },
router,
} = this.props;
return (
<div>
{location.pathname}, {router}
</div>
);
}
function ComponentWithRouter({ match, router }: Props) {
return (
<div>
{match.location.pathname}, {router}
</div>
);
}

const ComponentWithRouterContainer = withRouter(ComponentWithRouter);
Expand Down
2 changes: 2 additions & 0 deletions types/example-app/MainPage.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as React from 'react';

import { ComponentUsingRouter } from './ComponentUsingRouter';
import { ComponentWithRouter } from './ComponentWithRouter';

export function MainPage() {
return (
<div>
<ComponentWithRouter />
<ComponentUsingRouter />
</div>
);
}
8 changes: 5 additions & 3 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,16 @@ declare module 'found' {
onClick: (event: React.SyntheticEvent<any>) => void;
}

interface WithRouterProps {
interface RouterState {
match: Match;
router: Router;
}

function withRouter<Props extends WithRouterProps>(
function useRouter(): RouterState;

function withRouter<Props extends RouterState>(
Component: React.ComponentType<Props>,
): React.ComponentType<Omit<Props, keyof WithRouterProps>>;
): React.ComponentType<Omit<Props, keyof RouterState>>;

class RedirectException {
constructor(location: LocationDescriptor);
Expand Down
5 changes: 5 additions & 0 deletions types/useRouter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare module 'found/lib/useRouter' {
import { useRouter } from 'found';

export default useRouter;
}

0 comments on commit 3093fb0

Please sign in to comment.