pmndrs / drei

🥉 useful helpers for react-three-fiber
https://docs.pmnd.rs/drei
MIT License
8.36k stars 699 forks source link

OrbitControl has wrong prop type #730

Open kian00sh opened 2 years ago

kian00sh commented 2 years ago

Problem description:

Expected type of OrbitalControl.ref is defined as React.Ref<OrbitControls> | undefined whereas the actual return type of the prop is OrbitControlProps

Relevant code:

Code:

const controlRef = useRef<OrbitControlsProps>(null);

<OrbitControls
  enableDamping={false}
  maxPolarAngle={Math.PI / 2}
  minAzimuthAngle={Math.PI / 2}
  ref={controlRef}
/>

Error:

Type 'RefObject<OrbitControlsProps>' is not assignable to type 'Ref<OrbitControls> | undefined'.
  Type 'RefObject<OrbitControlsProps>' is not assignable to type 'RefObject<OrbitControls>'.
    Type 'OrbitControlsProps' is not assignable to type 'OrbitControls'.
      Types of property 'object' are incompatible.
        Type 'Camera | undefined' is not assignable to type 'Camera'.
          Type 'undefined' is not assignable to type 'Camera'.ts(2322)

Suggested solution:

Change the type to React.Ref<OrbitControlProps> | undefined

CodyJasonBennett commented 2 years ago

I think this is something to be done in user-land, OrbitControls will always write OrbitControlProps to ref.

You can try a few things like non-null assertions or adding null:

const controlRef = useRef<OrbitControlsProps>(null!)
//
const controlRef = useRef<OrbitControlsProps | null>(null)
bjornstar commented 2 years ago

Almost there:

import type { OrbitControls as OrbitControlsImpl } from 'three-stdlib'

const ref = useRef<OrbitControlsImpl>(null)

<OrbitControls ref={ref} />
github-actions[bot] commented 2 days ago

Thank you for contributing! We’re marking this issue as stale as a gentle reminder to revisit it and give it the attention it needs to move forward.

Any activity, like adding an update or comment, will automatically remove the stale label so it stays on our radar.

Feel free to reach out on Discord if you need support or feedback from the community. This issue will close automatically soon if there’s no further activity. Thank you for understanding and for being part of the project!