Next.JS, Typescript withRouter: "Type ... is not assignable to type 'IntrinsicAttributes & Pick<>'" in React

Viewed 1661

I'm new to TypeScript and I'm currently setting up a React application with Next.js, Redux, Jest, etc. However when running the application I'm getting a Type error which stops the application.

It seems problem is down to the props I'm passing in. This component is wrapped with a HOC 'withRouter' component from next which returns the same component with route information, i.e. {router: {pathname: /}}

My Component (Logo.tsx)

import React, { SFC } from 'react';
import { withRouter, WithRouterProps } from 'next/router';
import LogoStyled from './Logo.styled';

interface LogoProps {
    router: { pathname: string };
}

const Logo: SFC<LogoProps & WithRouterProps> = ({ router }) => {
    const { pathname } = router;
    return pathname === '/' ? (
        <LogoStyled>Home</LogoStyled>
    ) : (
        <LogoStyled>Not Home</LogoStyled>
    );
};

export default withRouter(Logo);

My Jest Test (Logo.test.tsx)

import React from 'react';
import { shallowToJson } from 'enzyme-to-json';
import 'jest-styled-components';
import { shallowWithTheme, mountWithTheme } from '../../../tests/testHelpers';

import Logo from './Logo';

const home = { pathname: '/' };
const about = { pathname: '/about' };

describe('Logo', () => {
    it('should render correctly', () => {
        const element = shallowWithTheme(<Logo router={home} />);
        expect(shallowToJson(element)).toMatchSnapshot();
    });
    it(`should display 'Home' for homepage`, () => {
        const element = mountWithTheme(<Logo router={home} />);
        expect(element.text()).toBe('Home');
    });
    it(`should display 'Not Home' for homepage`, () => {
        const element = mountWithTheme(<Logo router={about} />);
        expect(element.text()).toBe('Not Home');
    });
});

Results in Error:

components/atoms/Logo/Logo.test.tsx:13:43 -
error TS2322: Type '{ router: { pathname: string; }; }'
is not assignable to type 'IntrinsicAttributes & Pick<LogoProps &
 WithRouterProps<Record<string, string | string[]>>, never> & { children?: ReactNode; }'.

Property 'router' does not exist on type 'IntrinsicAttributes &
Pick<LogoProps & WithRouterProps<Record<string, string | string[]>>,
never> & { children?: ReactNode; }'.

I'm guessing this quite an obvious thing I'm missing, but I'm struggling to make sense out of this at the moment. Thank you for any help you can give.

0 Answers
Related