Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property does not exist on type 'IntrinsicAttributes & { children?: ReactNode; }'

I have react project created by Create-React-App having following packages (mentioning packages related to my issue) :

"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.1",
"typescript": "^3.9.2",
"@typescript-eslint/eslint-plugin": "^2.33.0",
"@typescript-eslint/parser": "^2.33.0"

i have created a simple HOC (which does nothing as of now, but i'll add my logic later) like this :

type Props = {
    [key: string]: any;
};


const connect = function (Component: FunctionComponent): FunctionComponent {
    const ComponentWrapper = function (props: Props): ReactElement {
        return <Component {...props} />;
    };

    return ComponentWrapper;
};

and exported my component like this :

    const Test: FunctionComponent<Props> = function ({ message }: Props) {
        return (
            <div>{message}</div>
        );
    };


export default connect(Test);

and using this component like this :

<Test message="Testing message" />

But getting error in compiler :

Type '{ message: string; }' is not assignable to type 'IntrinsicAttributes & { children?: ReactNode; }'.
  Property 'message' does not exist on type 'IntrinsicAttributes & { children?: ReactNode; }'.  TS2322

I have tried what people have suggested in other similar Stack Overflow questions and article found on Google, but nothing worked till yet.

like image 962
Ritesh Avatar asked May 17 '20 14:05

Ritesh


People also ask

What is IntrinsicAttributes?

IntrinsicAttributes interface can be used to specify extra properties used by the JSX framework which are not generally used by the components' props or arguments - for instance key in React. Specializing further, the generic JSX.

Does not exist on type readonly?

The React. js error "Property does not exist on type 'Readonly<{}>'" occurs when we try to access the props or state of a class component which we haven't typed. To solve the error, use the generic on the React. Component class to type the props or state objects of the class.

Is missing the following properties from type?

The error "Type is missing the following properties from type" occurs when the type we assign to a variable is missing some of the properties the actual type of the variable expects. To solve the error, make sure to specify all of the required properties on the object.


1 Answers

// This is the piece we were missing --------------------v
const connect = function (Component: React.FC): React.FC<Props> {
    const ComponentWrapper = function (props: Props): JSX.Element {
        return <Component {...props} />;
    };

    return ComponentWrapper;
};

and after restarting the compiler it'll work fine.

The type of the return value of the connect function is a functional component that requires Props, not a bare functional component.

See also the cheatsheet

like image 156
Max Arzamastsev Avatar answered Sep 22 '22 18:09

Max Arzamastsev