Skip to content

Commit

Permalink
fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
taion committed Jun 20, 2019
1 parent 68182aa commit 680d5da
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/BaseLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import warning from 'warning';
import { routerShape } from './PropTypes';

const propTypes = {
as: PropTypes.elementType,
as: PropTypes.elementType.isRequired,
to: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired,
match: PropTypes.object.isRequired,
activeClassName: PropTypes.string,
activeStyle: PropTypes.object,
activePropName: PropTypes.string,
router: routerShape.isRequired,
exact: PropTypes.bool,
exact: PropTypes.bool.isRequired,
target: PropTypes.string,
onClick: PropTypes.func,
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
Expand Down
2 changes: 1 addition & 1 deletion types/example-app/CustomLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';

interface Props {
href: string;
active?: boolean;
active: boolean;
onClick: (event: React.SyntheticEvent<any>) => void;
children: React.ReactNode;
}
Expand Down
76 changes: 49 additions & 27 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,43 +333,65 @@ declare module 'found' {

class Redirect extends React.Component<RedirectProps> {}

interface BaseLinkProps
extends React.AnchorHTMLAttributes<HTMLAnchorElement> {
interface LinkPropsCommon {
to: LocationDescriptor;
// match: Match, provided by withRouter
activeClassName?: string;
activeStyle?: any;
activePropName?: string;
// router: Router, provided by withRouter
exact?: boolean;
target?: string;
children?:
| React.ReactNode
| ((linkRenderArgs: {
href: string;
active: boolean;
onClick: (event: React.SyntheticEvent<any>) => void;
}) => React.ReactNode);
onClick?: (event: React.SyntheticEvent<any>) => void;
}

type ReplaceProps<Inner extends React.ElementType, P> = Omit<
React.ComponentProps<Inner>,
keyof P
interface LinkPropsNodeChild extends LinkPropsCommon {
activeClassName?: string;
activeStyle?: {};
children?: React.ReactNode;
}

type ReplaceProps<TInner extends React.ElementType, TProps> = Omit<
React.ComponentProps<TInner>,
keyof TProps
> &
P;

type LinkProps<As extends React.ElementType> = ReplaceProps<
As,
BaseLinkProps
> & { as?: As };

class Link<As extends React.ElementType = 'a'> extends React.Component<
LinkProps<As>
> {
onClick: (event: React.SyntheticEvent<any>) => void;
readonly props: LinkProps<As>;
TProps;

type LinkPropsSimple = ReplaceProps<'a', LinkPropsNodeChild>;

type LinkPropsWithAs<TInner extends React.ElementType> = ReplaceProps<
TInner,
LinkPropsNodeChild & { as: TInner }
>;

type LinkPropsWithActivePropName<
TInner extends React.ElementType<
{ [activePropName in TActivePropName]: boolean }
>,
TActivePropName extends string
> = ReplaceProps<
TInner,
LinkPropsNodeChild & { as: TInner; activePropName: TActivePropName }
>;

interface LinkPropsWithFunctionChild extends LinkPropsCommon {
children: (linkRenderArgs: {
href: string;
active: boolean;
onClick: (event: React.SyntheticEvent<any>) => void;
}) => React.ReactNode;
}

class Link<
TInner extends React.ElementType = never,
TInnerWithActivePropName extends React.ElementType<
{ [activePropName in TActivePropName]: boolean }
> = never,
TActivePropName extends string = never
> extends React.Component<
| LinkPropsWithFunctionChild
| LinkPropsWithActivePropName<TInnerWithActivePropName, TActivePropName>
| LinkPropsWithAs<TInner>
| LinkPropsSimple
> {}

interface RouterState {
match: Match;
router: Router;
Expand Down

0 comments on commit 680d5da

Please sign in to comment.