Sign in
Build 10x products in minutes by chatting with AI - beyond just a prototype.
This blog is tailored to help you understand and resolve the error 'cannot find namespace jsx'.
When working with React and TypeScript, few things disrupt development more than unexpected compiler errors. One that continues to cause confusion—especially in modern setups—is:
1Cannot find namespace 'JSX'
With the release of React 19 and TypeScript 5.7+, this error has become more prominent due to changes in how the JSX namespace is handled. This guide walks you through exactly what’s changed, why it matters, and how to fix it fast—so you can get back to shipping code without mysterious type errors.
Traditionally, TypeScript provided a global JSX
namespace for typing JSX elements. However, starting in TypeScript 5.7, this global namespace is no longer automatically available unless explicitly referenced.
In modern React projects using automatic JSX runtime ("jsx": "react-jsx"
), JSX types must now be accessed explicitly via React.JSX
, not just JSX
.
React.JSX
Instead of JSX
Update type annotations that previously used:
1const element: JSX.Element = <MyComponent />
To:
1const element: React.JSX.Element = <MyComponent />;
This reflects the updated structure of React’s types and prevents the “Cannot find namespace 'JSX'” error.
tsconfig.json
Your tsconfig.json
should be configured as follows to align with modern React and TypeScript practices:
1{ 2 "compilerOptions": { 3 "jsx": "react-jsx", 4 "lib": ["dom", "esnext"], 5 "esModuleInterop": true, 6 "skipLibCheck": true 7 } 8}
What These Settings Do:
"jsx": "react-jsx"
: Enables React 17+ automatic JSX transform."lib": ["dom", "esnext"]
: Provides DOM and modern JavaScript features."esModuleInterop": true
: Ensures compatibility with CommonJS-style modules."skipLibCheck": true
: Skips type checking for third-party dependencies (helpful when they reference global JSX
improperly)..tsx
for JSX FilesAny file containing JSX must use the .tsx
extension. TypeScript won’t process JSX syntax correctly in .ts
or .js
files.
✅ Correct: MyComponent.tsx
❌ Incorrect: MyComponent.ts
Ensure that React’s type definitions are installed and up to date:
1npm install --save-dev @types/react @types/react-dom
These packages provide necessary type declarations for React components and JSX elements.
Some third-party libraries may still rely on the deprecated global JSX
namespace. This can lead to persistent type errors even in correctly configured projects.
What You Can Do:
Update the library: Check for the latest version of libraries like styled-components
or Prismic
that fix these issues.
Temporarily bypass the error using:
1"skipLibCheck": true
This suppresses type-checking for .d.ts
files in node_modules
.
Legacy projects often use JSX.Element
. Refactor as follows:
JSX.Element
→ React.JSX.Element
keyof JSX.IntrinsicElements
→ keyof React.JSX.IntrinsicElements
Also, review any custom d.ts
files or internal types that reference JSX
globally.
typeRoots
or types
In rare cases, explicitly setting typeRoots
and types
can help:
1{ 2 "compilerOptions": { 3 "typeRoots": ["./node_modules/@types"], 4 "types": ["react", "react-dom"] 5 } 6}
Use this if your project has non-standard structure or custom tooling.
The "Cannot find namespace 'JSX'" error reflects the evolving nature of the React and TypeScript ecosystem. With React 19 and TypeScript 5.7+, type declarations are more modular, and your code needs to explicitly reference the React.JSX
namespace.
To stay ahead:
tsconfig.json
settings.Proactively maintaining your project setup ensures a smoother, error-free development experience in the fast-moving JavaScript landscape.