diff --git a/types/next-server/router.d.ts b/types/next-server/router.d.ts index dff4f13a79..190138e3ed 100644 --- a/types/next-server/router.d.ts +++ b/types/next-server/router.d.ts @@ -82,13 +82,19 @@ export interface WithRouterProps { router: SingletonRouter; } +/** + * Remove properties `K` from `T`. + * + * @internal + */ +export type Omit = T extends any ? Pick> : never; + // Manually disabling the no-unnecessary-generics rule so users can -// retain type inference if they warp their component in withRouter +// retain type inference if they wrap their component in withRouter // without defining props explicitly export function withRouter( - // tslint:disable-next-line:no-unnecessary-generics - Component: React.ComponentType> -): React.ComponentType; + Component: React.ComponentType> +): React.ComponentType>>; declare const Router: SingletonRouter; export default Router; diff --git a/types/next-server/test/next-server-router-tests.tsx b/types/next-server/test/next-server-router-tests.tsx index 8b4397baad..bac699b4e7 100644 --- a/types/next-server/test/next-server-router-tests.tsx +++ b/types/next-server/test/next-server-router-tests.tsx @@ -70,14 +70,14 @@ Router.prefetch("/route").then(Component => { const element = ; }); -interface TestComponentProps { +interface TestComponentProps extends WithRouterProps { testValue: string; } -class TestComponent extends React.Component { +class TestComponent extends React.Component { state = { ready: false }; - constructor(props: TestComponentProps & WithRouterProps) { + constructor(props: TestComponentProps) { super(props); props.router.ready(() => { this.setState({ ready: true }); @@ -97,12 +97,57 @@ class TestComponent extends React.Component { + state = { ready: false }; + + constructor(props: TestComponent2Props) { + super(props); + props.router.ready(() => { + this.setState({ ready: true }); + }); + } + + render() { + return ( +
+

{this.state.ready ? 'Ready' : 'Not Ready'}

+

Route: {this.props.router.route}

+

Another prop: {this.props.testValue}

+
+ ); + } +} + +const TestComponent2WithRouter = withRouter(TestComponent2); +const res = ; + interface TestSFCQuery { test?: string; } -interface TestSFCProps extends WithRouterProps { } +interface TestSFCProps extends WithRouterProps { + testProp: string; +} const TestSFC: React.SFC = ({ router }) => { return
{router.query && router.query.test}
; }; +const TestSFCComponent = withRouter(TestSFC); + +const res2 = ; + +const TestSFC2 = withRouter(({ router }) => { + return
{router.query && router.query.test}
; +}); + +const res3 = ; + +const TestSFC3 = withRouter(({ router }) => { + return
{router.query && router.query.test}
; +}); + +const res4 = ;