Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property ... does not exist on type 'IntrinsicAttributes & ...'

I changed my .js file to a typescript file .tsx. I have the following function defined within the file:

function MyCard(param1: ObjectDto, toggleFunction: any) {}

I am using this function in another .tsx file like this

<MyCard param1={param1Value} toggleFunction={myToggleFunction} />

But I am getting the following error:

Type '{ param1: ObjectDto; toggleFunction: (index: any) => void; }' is not assignable to type 'IntrinsicAttributes & ObjectDto'. Property 'param1' does not exist on type 'IntrinsicAttributes & ObjectDto'.

This worked fine before changing to the Typescript format. How can I fix this for Typescript?

like image 791
doorman Avatar asked Mar 09 '19 09:03

doorman


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.

What type does useRef return?

useRef returns a mutable ref object whose .current property is initialized to the passed argument ( initialValue ). The returned object will persist for the full lifetime of the component. Essentially, useRef is like a “box” that can hold a mutable value in its .current property.

How do I give someone a useRef?

To use useRef with TypeScript, it is actually pretty easy, all you need to do is provide the type that you want to the function via open and closed chevrons like you would with other React hooks like so: const myRef = useRef<number>(0) .

What is FC in React?

FC provides an implicit definition of children. This means that defining a component with React.FC causes it to implicitly take children of type ReactNode . Even if your component doesn't allow children, using React.FC opens your component's props to it: JavaScript.


2 Answers

Components have only one parameter which is the props object, therefore your function should look something like this:

function MyCard(props: { param1: ObjectDto, toggleFunction: any }) {}

The type system is trying to match the properties with the first parameter of your function, which is of type ObjectDto. That's why you get error

is not assignable to type 'IntrinsicAttributes & ObjectDto'

like image 142
Sulthan Avatar answered Oct 08 '22 17:10

Sulthan


Did you make an interface for your component "MyCard"?

Like this:

interface MyCodeParams {
    param1: ObjectDto
    toggleFunction: (param: any) => void
}

Also, i think toggleFunction: (param?: any) => void is the right type because your function may have parameters so you should type your function like this.

like image 7
Sinane Avatar answered Oct 08 '22 17:10

Sinane