react-three-fiber
react-three-fiber copied to clipboard
Draft: Canvas type improvements
Improve typing on createRoot by declaring that it handles HTMLCanvasElement | OffscreenCanvas. This is more expressive than accepting any subclass of Element and allows some generic type params to be removed.
Two reasons this is a draft:
- Though removing the generic type param from
createRootseems like a clear DX improvement, the function is part of the publicly documented API of r3f so this could be considered a small breaking change - To discuss approaches on how to source the
OffscreenCanvastyping. Currently, I import it from the three definitely typed definitions but the def there seems non-ideal. I was a bit surprised that the type isn't defined indom.lib.d.ts. Poking around, it seems that it is, however, defined inlib.webworker.d.tsmeaning that it's possible to modify thetsconfigto includeWebWorker. I wouldn't recommend this approach since generally the code in this repo is intended for the main thread so making worker-side types universally available is probably a bad idea. Open to suggestions here.
This pull request is automatically built and testable in CodeSandbox.
To see build info of the built libraries, click here or the icon next to each commit SHA.
Latest deployment of this branch, based on commit 056492fe47681dbf992db29a630e510bc3c7a5f6:
| Sandbox | Source |
|---|---|
| example | Configuration |
Keen to hear your thoughts on typing OffscreenCanvas @joshuaellis (thanks for taking over the three.js typings btw)
I'm looking to pick this PR into v9 (#2331), although the only withstanding issue would be with the question of how to source OffscreenCanvas. I'm very wary of relying on types' dependencies as it undermines our ability to follow semver, and we've already been bitten by this with WebXR which necessitates the major bump in v9.
I coincidentally mirrored this in #2770 and #2774, so merging as complete. I'll relegate removing the redundant generics from RenderProps etc. to v9.