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

[react-router] Fix #38271 (withRouter fails with ComponentType starting in 3.6.2) #38326

Merged
merged 2 commits into from Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions types/react-router/index.d.ts
Expand Up @@ -143,8 +143,12 @@ export interface WithRouterStatics<C extends React.ComponentType<any>> {
// they are decorating. Due to this, if you are using @withRouter decorator in your code,
// you will see a bunch of errors from TypeScript. The current workaround is to use withRouter() as a function call
// on a separate line instead of as a decorator.
export function withRouter<P extends RouteComponentProps<any>, C extends React.ComponentType<P>>(
component: C & React.ComponentType<P>,
): React.ComponentClass<Omit<P, keyof RouteComponentProps<any>> & WithRouterProps<C>> & WithRouterStatics<C>;
export function withRouter<C extends React.ComponentType<RouteComponentProps>>(
component: C,
): React.ComponentClass<
Omit<React.ComponentProps<C>, keyof RouteComponentProps> & WithRouterProps<C>,
never
> &
WithRouterStatics<C>;

export const __RouterContext: React.Context<RouteComponentProps>;
8 changes: 8 additions & 0 deletions types/react-router/test/WithRouter.tsx
Expand Up @@ -9,13 +9,21 @@ const ComponentFunction = (props: TOwnProps) => (
<h2>Welcome {props.username}</h2>
);

const FunctionComponent: React.FunctionComponent<TOwnProps> = props => (
<h2>Welcome {props.username}</h2>
);

declare const Component: React.ComponentType<TOwnProps>;

class ComponentClass extends React.Component<TOwnProps> {
render() {
return <h2>Welcome {this.props.username}</h2>;
}
}

const WithRouterComponentFunction = withRouter(ComponentFunction);
const WithRouterFunctionComponent = withRouter(FunctionComponent);
const WithRouterComponent = withRouter(Component);
const WithRouterComponentClass = withRouter(ComponentClass);
WithRouterComponentClass.WrappedComponent; // $ExpectType typeof ComponentClass

Expand Down