I have been working with react.js
for a while. Recently, i start seeing an error like:
Type '{}' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes'
I don't know what's going on. I googled about it and what i found other's saying about is that it is an issue with props
. Here is a scenario that i saw. Rather than:
<MyComponent action={this.state.action} />
the following work's fine:
<MyComponent {...props} />
I want to understand this thing that what is IntrinsicAttributes
and IntrinsicClassAttributes
in react
. Can you please provide me a detailed answer that why such type of error occures and what are these really?
js error "Type {children: Element} has no properties in common with type IntrinsicAttributes" occurs when we try to pass a children prop to a component that doesn't take any props. To solve the error define and type the props on the component.
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.
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.
To pass a function as props in React TypeScript: Define a type for the function property in the component's interface. Define the function in the parent component. Pass the function as a prop to the child component.
Interfaces implemented in TypeScript. They influence React and .jsxs interaction with the DOM elements, while working with Custom Components.
Lets assume you have
interface BarProps {
foo: string;
}
class Bar extends React.Component<BarProps> {
...
}
If you try to render it
render() {
return (
<form>
<Bar />
</form>
);
}
You'll get a similar error; since you're violating the interface typecheck.
The Component should have a mandatory props i.e BarProps
passed as an argument here.
Instead to make it work, you'll need to do something like..
render() {
return (
<form>
<Bar foo="Jack Daniel's"/>
</form>
);
}
or you could remove it from the Component definition itself.
class Bar extends React.Component<> {
...
}
or make the foo
optional..
interface BarProps{
foo?: string;
}
In your case you're passing an unknown prop i.e action
which has probably not been defined in your Component.
When you call your component like <MyComponent {...props} />
- it essentially means, import all available props.
When you explicitly call action
prop like <MyComponent action={this.state.action} />
it throws the big fat error.
The mentioned errors are quite notorious. You can find more insights in this debugging guide for React and TypeScript which highlights these errors and share the possible fix.
You can read more about the implementation of IntrinsicAttributes
and IntrinsicClassAttributes
in this repository
Digging into Typescript's checker.ts it works likely because the block where the assignment to intrinsicAttributes is made is not needed to be executed, since there are no explicit JsxAttributes to compare for that component's invocation (isComparingJsxAttributes may be false). Try debugging Typescript's source code if you really need to find out if this is the case.
Just take an Example that Here typescript expects okay something is being passed only to be deconstructed when component will mount.
<MyComponent {...props} />
But here typescript takes this.state.action as undefined or maybe null because it is never sure there'll be a valid value passed.
<MyComponent action={this.state.action} />
Even if you have got the actions prop's type right. it still dosent know whether it has value or not hance see it as {}
which is an empty object and cant be assigned to action.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With