Shivan M.
—When using a component provided by a third-party package, you may encounter an error related to React types when trying to use those components. This error will only occur when attempting to build or run your project on a different environment or machine to your development environment.
The error is:
Type '{}' is not assignable to type 'ReactNode'
The top of the stack trace will state:
Type error: 'ThirdPartyComponent` cannot be used as a JSX component.
This error usually occurs because the @types/react
package version is incorrect. Many popular libraries (including @types/react-dom
) set the version of @types/react
to *
- the most recent major release. The package resolution in the build step of your project can cause incompatible versions of @types/react
to be installed in your project resulting in the error.
The core issue is not with the React typings, but instead with how dependencies are represented in Typescript. The long-term discussion about this problem can be found here. For the moment, we have to rely on workarounds.
The solution to this problem will depend on the package manager you use for your project.
With yarn, you have two possible solutions.
You can remove duplicate resolutions of the @types/react
package by running the following command:
npx yarn-deduplicate --packages @types/react
This will work as long the projects that depend on @types/react
also support the version of React that your project is running.
For Yarn v2, the solution is similar and uses the dedup
command:
yarn dedup @types/react
package.json
You can specify strict resolution restrictions for dependencies of your dependencies by adding the following to your package.json
file:
"resolutions": { "@types/react": "^18.0.0", }
You can use the overrides
directive to explicitly set the version of the @types/react
package to be used in your package.json
:
{ "overrides": { "@types/react": "^18.0.0" } }
Similarly to the npm
solution above, for pnpm
, you can set the version of @types/react
to be used in your package.json
:
"pnpm": { "overrides": { "@types/react": "^18.0.0" } }
The overrides directive for pnpm
as explained in their documentation.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.