0

I have a React HOC structured like this:

import React from 'react';
import { CookiesConsumer } from './index';

const withCookies = (WrappedComponent: any) => (props: any) => (
  <CookiesConsumer>
    {cookies => (
      <WrappedComponent
        {...props}
        cookiesContext={cookies}
      />
    )}
  </CookiesConsumer>
);

export default withCookies;

I get 2 Typescript eslint warnings on this that I'd like to fix. Both on the any type declarations:

Unexpected any. Specify a different type.eslint(@typescript-eslint/no-explicit-any)

For the props type, I don't know what properties or value types might be in the props object, so how can I define it better than any?

For the WrappedComponent type I've tried types like ReactNode or ReactElement but that throws a new error on the actual usage of : JSX element type 'WrappedComponent' does not have any construct or call signatures.

1 Answer 1

7
import React from 'react';
import { CookiesConsumer } from './index';

interface withCookiesProps {
    someProp: boolean;
  }


const withCookies = <P extends object>(WrappedComponent: React.ComponentType<P>) => (props: P & withCookiesProps) => (
  <CookiesConsumer>
    {cookies => (
      <WrappedComponent
        {...props}
        cookiesContext={cookies}
      />
    )}
  </CookiesConsumer>
);

export default withCookies;

Refernence

Sign up to request clarification or add additional context in comments.

3 Comments

Feel like I'm 95% there with this but struggling a little because the example you give is a bit different. Would you be able to provide an example where the props are unknown like my component?
Let me know if it's ok, if no I will check it out again.
OK I think we're there! My example doesn't have any props for withCookies so I removed that from your example but the rest seems to get rid of existing errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.