Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overriding JSX.Element in global namespace causing type errors #2301

Closed
cmpdc opened this issue Jan 14, 2025 · 1 comment
Closed

Overriding JSX.Element in global namespace causing type errors #2301

cmpdc opened this issue Jan 14, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@cmpdc
Copy link

cmpdc commented Jan 14, 2025

  • three version: ^0.172.0
  • @react-three/fiber version: ^8.17.12
  • @react-three/drei version: ^9.120.8
  • node version: v20.12.2
  • npm (or yarn) version: 10.9.0

Problem description:

What happens: Element in JSX.Element is missing due to a namespace JSX override. As shown here: https://github.com/pmndrs/drei/blob/346326928346612f5d12f1f291699ce78aa0e5fd/src/core/MeshDistortMaterial.tsx#L19C1-L25C2

Why this is problematic: It affects all files importing JSX types globally and breaks React's expected typings.

Relevant code:

Snippet from my codebase:

interface ThreeJSProject {
  content: () => JSX.Element;
  name: string;
}

This causes the TypeScript error: Namespace 'global.JSX' has no exported member 'Element'. ts(2694)
The expected behavior should allow JSX.Element to be used without errors, even after extending the JSX.IntrinsicElements.

Suggested solution:

  • Extend the JSX namespace rather than replacing it entirely.
  • Add documentation on how to restore compatibility when using custom types.

As a stopgap of the issue, I currently have the following in my own global.d.ts that fixes the issue:

import React from "react";

declare global {
	namespace JSX {
		interface IntrinsicElements {
			distortMaterialImpl?: any;
		}

		type Element = React.ReactNode;
	}
}
@cmpdc cmpdc added the bug Something isn't working label Jan 14, 2025
@CodyJasonBennett
Copy link
Member

This is the intended way to augment React JSX in React <19. If you are using React 19, see #2260.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants