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

Type 'RefObject<DirectionalLight>' is not assignable to type 'MutableRefObject<Object3D<Event>> #1348

Open
steelx opened this issue Mar 16, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@steelx
Copy link

steelx commented Mar 16, 2023

"@react-three/drei": "^9.57.0",
"@react-three/fiber": "^8.11.5",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"three": "^0.150.1"

Problem description:

how to use useHelper with DirectionalLight in typescript.
I get similar type error when using TransformControls as well.
image

Relevant code:

import { OrbitControls, useHelper } from "@react-three/drei";
import { Canvas } from "@react-three/fiber";
import { Perf } from "r3f-perf";
import { useRef, type PropsWithChildren } from "react";
import { DirectionalLightHelper, type DirectionalLight } from "three";

const LightWithHelper = () => {
	const dirLightRef = useRef<DirectionalLight | null>(null);
	if (dirLightRef !== null) {
		useHelper(dirLightRef, DirectionalLightHelper, 1, 0xff0000);
	}

	return (
		<directionalLight
			castShadow
			position={[1, 5, 5]}
			intensity={1.2}
			ref={dirLightRef}
		/>
	);
};

const FiberContainer = ({ children }: PropsWithChildren) => {
	return (
		<Canvas
			camera={{
				position: [14, 12, 5],
				fov: 40,
				near: 1,
				far: 200,
				zoom: 1,
			}}
			shadows
		>
			<Perf />
			<axesHelper />
			<OrbitControls
				makeDefault
				minDistance={1}
				maxDistance={200}
				enablePan={false}
				enableZoom={true}
			/>
			<ambientLight intensity={0.3} />
			<LightWithHelper />
			<color args={["ivory"]} attach="background" />
			{children}
		</Canvas>
	);
};

export default FiberContainer;

Suggested solution:

can you share any typescript examples ?

issue with TransformControls as well

image

@steelx steelx added the bug Something isn't working label Mar 16, 2023
@yardenxr
Copy link

it is relevant also for OrbitControls and i guess for all places which are using
type Ref<T> = RefCallback<T> | RefObject<T> | null;

@thomasryu
Copy link

I am also having the problem with TransformControls
when using drei with TypeScript

  Type 'RefObject<Mesh<BufferGeometry<NormalBufferAttributes>, Material | Material[], Object3DEventMap>>' is not assignable to type 'MutableRefObject<Object3D<Object3DEventMap>>'.
    Types of property 'current' are incompatible.
      Type 'Mesh<BufferGeometry<NormalBufferAttributes>, Material | Material[], Object3DEventMap> | null' is not assignable to type 'Object3D<Object3DEventMap>'.
        Type 'null' is not assignable to type 'Object3D<Object3DEventMap>'.ts(2322)
TransformControls.d.ts(7, 5): The expected type comes from property 'object' which is declared here on type 'IntrinsicAttributes & Omit<TransformControlsProps, "ref"> & RefAttributes<TransformControls<Camera>>'

@EngelbertTeh
Copy link

EngelbertTeh commented Dec 10, 2023

So is there a solution?

In the meantime i am using the any type

@lewsmith
Copy link

lewsmith commented Mar 7, 2024

Same issue:

Argument of type 'RefObject<DirectionalLight>' is not assignable to parameter of type 'MutableRefObject<Object3D<Object3DEventMap>> | Falsy'.
  Type 'RefObject<DirectionalLight>' is not assignable to type 'MutableRefObject<Object3D<Object3DEventMap>>'.
    Types of property 'current' are incompatible.
      Type 'DirectionalLight | null' is not assignable to type 'Object3D<Object3DEventMap>'.
        Type 'null' is not assignable to type 'Object3D<Object3DEventMap>'.ts(2345)

Any solutions?

UPDATE:

Disabling strictNullChecks in the tsconfig.json fixes the error.

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

5 participants