React-three-fiber: TypeScript typings don't allow passing camera position as array

Created on 7 Oct 2019  路  6Comments  路  Source: pmndrs/react-three-fiber

For the following code

<Canvas camera={{ position: [0, 0, 20] }}>
  // ...
</Canvas>

TypeScript complains that:

Type '{ position: number[]; }' is not assignable to type 'Partial<OrthographicCamera & PerspectiveCamera>'.
  Types of property 'position' are incompatible.
    Type 'number[]' is missing the following properties from type 'Vector3': x, y, z, isVector3, and 68 more.ts(2322)
canvas.d.ts(80, 3): The expected type comes from property 'camera' which is declared here on type 'IntrinsicAttributes & CanvasProps'

For those that bump into this themselves, an easy workaround is to assert the type as any, as in camera={{ position: [0, 0, 20] as any }}.

Most helpful comment

this should work now ...

All 6 comments

do you know why? im still not so good with TS. shouldn't Partial be enough? these two most certainly have the position prop in threejs.

It's because three.js is expecting a Vector3 there and Vector3 !== [number, number, number].

I think the types at three.js need to be updated to allow the shorthand initialiser.

If you want to do it "By the book (tm)", here's how:

<Canvas camera={{ position: new THREE.Vector3(0, 0, 20) }} />

i think its because its using applyProps, so mabye it should be a partial of

ReactThreeFiber.Object3DNode
perspectiveCamera: ReactThreeFiber.Object3DNode
orthographicCamera: ReactThreeFiber.Object3DNode

like in the three-types file.

@birkir I quickly looked at the Threejs documentation and the shorthand is not documented anywhere, so I think the Threejs typings are correct. @drcmda is the array shorthand for Vectors also in Threejs or is it specific to react-three-fiber? If it is in Three, but not officially supported, it might be better still to use Vector3 in the react-three-fiber examples.

Edit: never mind, forgot to refresh my browser before commenting. :D

yes, its specific to r3f. we have the types, that's why mesh position={[1,2,3]} works, just needs to apply to the canvas props as well i guess.

this should work now ...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

flxsu picture flxsu  路  4Comments

gtolarc picture gtolarc  路  4Comments

jamestubman picture jamestubman  路  6Comments

gaearon picture gaearon  路  3Comments

objectisundefined picture objectisundefined  路  4Comments