From 37d97cd78f2a7b811414def0ffc208058b34f78a Mon Sep 17 00:00:00 2001 From: Karol Majewski <20233319+karol-majewski@users.noreply.github.com> Date: Tue, 17 Sep 2019 19:33:06 +0200 Subject: [PATCH] [react-router] Fix #38271 (`withRouter` fails with `ComponentType` starting in 3.6.2) (#38326) * Make withRouter compatible with ComponentType Fixes #38271. * Add new baselines Co-authored-by: Oliver Joseph Ash --- types/react-router/index.d.ts | 10 +++++++--- types/react-router/test/WithRouter.tsx | 8 ++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/types/react-router/index.d.ts b/types/react-router/index.d.ts index 3746fa5c50d53e..050f73725ff699 100644 --- a/types/react-router/index.d.ts +++ b/types/react-router/index.d.ts @@ -143,8 +143,12 @@ export interface WithRouterStatics> { // 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

, C extends React.ComponentType

>( - component: C & React.ComponentType

, -): React.ComponentClass> & WithRouterProps> & WithRouterStatics; +export function withRouter>( + component: C, +): React.ComponentClass< + Omit, keyof RouteComponentProps> & WithRouterProps, + never +> & + WithRouterStatics; export const __RouterContext: React.Context; diff --git a/types/react-router/test/WithRouter.tsx b/types/react-router/test/WithRouter.tsx index fb895be46173d2..0f9f684a2cd589 100644 --- a/types/react-router/test/WithRouter.tsx +++ b/types/react-router/test/WithRouter.tsx @@ -9,6 +9,12 @@ const ComponentFunction = (props: TOwnProps) => (

Welcome {props.username}

); +const FunctionComponent: React.FunctionComponent = props => ( +

Welcome {props.username}

+); + +declare const Component: React.ComponentType; + class ComponentClass extends React.Component { render() { return

Welcome {this.props.username}

; @@ -16,6 +22,8 @@ class ComponentClass extends React.Component { } const WithRouterComponentFunction = withRouter(ComponentFunction); +const WithRouterFunctionComponent = withRouter(FunctionComponent); +const WithRouterComponent = withRouter(Component); const WithRouterComponentClass = withRouter(ComponentClass); WithRouterComponentClass.WrappedComponent; // $ExpectType typeof ComponentClass