-
Notifications
You must be signed in to change notification settings - Fork 120
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
fix: controls event dispatcher types #388
fix: controls event dispatcher types #388
Conversation
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. |
hi @RodrigoHamuy, I've just tried upgrading drei to latest three-stdlib but I have TS warnings: |
Hi @abernier, really sorry about that. I probably need some help as I am not sure what should we do. This PR made a copy of But in reality the original My understanding is that because Is there another way to do this? I will have a play on my side too, but not really sure we can do anything else apart from actually asking Maybe we should revert this commit in the meantime, sorry for the trouble. |
I believe that type error comes from R3F since now the types diverge. Maybe we can loosen it there. |
or couldn't we let r3f uses stdlib's EventDispatcher (for nb: it will add a (dev-)dep to stdlib for fiber |
All we need is a small interface that is compatible with both types, although it doesn't matter whether |
|
@RodrigoHamuy yes, if you clone pmndrs/drei @ master and you should have the same TS errors
@CodyJasonBennett like this?: # pmndrs/react-three-fiber/blob/master/packages/fiber/src/core/store.ts
- controls: THREE.EventDispatcher | null;
+ controls: Omit<THREE.EventDispatcher, 'addEventListener' | 'hasEventListener' | "removeEventListener"> | null; if I understand correctly, this means a user can no longer |
@CodyJasonBennett @abernier here are a few options I think would solve this:
I prefer option one, is simple and avoids having to maintain a copy of a class that hasn't changed in years ( Happy to also show a draft PR of Mainly I believe is just in https://github.com/RodrigoHamuy/drei/blob/master/src/core/OrbitControls.tsx#L7 - export type OrbitControlsChangeEvent = Event & {
- target: EventTarget & { object: Camera }
- }
+ type ExtractCallback<T, E extends string> = T extends { addEventListener(event: E, callback: infer C): void } ? C : never;
+ export type OrbitControlsChangeEvent = Parameters<ExtractCallback<OrbitControlsImpl, 'change'>>[0] |
I see that the transformControls is a bit more tricky as copying the native three implementation would require to put a lot of transformControls property to public so that it's "transformControlsRoot" has access to them. |
@CodyJasonBennett @abernier any thoughts on the 2 alternative solutions mentioned here? Neither of them would require changing |
iiuc 1. depends on three-types/three-ts-types#1398, whether it is merged or not, is that it? |
i mean: it's not possible to revert if three-types/three-ts-types#1398 is not merged, am i right? |
Maybe let's open an issue since a revert is unlikely (it is equally breaking to change your mind), and we'd all be interested in a workaround or proper migration. I still think loosening the type on R3F's side would be the best course of action since we don't actually care about controls implementing events at all. It can extend as simple as an opaque object type. |
@CodyJasonBennett ticket added to R3F: pmndrs/react-three-fiber#3416 Otherwise we can repurpose #387 as the fix was incomplete.
Are you suggesting this? I'm worried bellow change will introduce a breaking changes for anybody that expects // file: https://github.com/pmndrs/react-three-fiber/blob/master/packages/fiber/src/core/store.ts#L112
- controls: THREE.EventDispatcher | null
+ controls: {} | null Personally, I think that if reverting both this PR and |
I meant either an opaque type that implemented event listener methods without generics or an object like that. This still might be too strict since people may implement their own interface with parameters; again, I think // file: https://github.com/pmndrs/react-three-fiber/blob/master/packages/fiber/src/core/store.ts#L112
- controls: THREE.EventDispatcher | null
+ controls: {
+ addEventListener(type: string, listener: (...args: any[]) => void): void
+ hasEventListener(type: string, listener: (...args: any[]) => void): boolean
+ removeEventListener(type: string, listener: (...args: any[]) => void): void
+ dispatchEvent(event: any): void
+} | null If #392 can create a compatible overload, then let's roll with that. Still, I'm unhappy with R3F taking such opinions over user code. |
@CodyJasonBennett fair enough. In that case I leave it with you to decide between these (I didn't add the loosing types of the
My personal preference is still option b since it reduces bundle size (we don't need to keep our copy of EventDistpacher), we stay more closely aligned with three/examples, and (unless people updated very very recently) it means no changes at all for them or our APIs. |
Fixes #387 by maintaining our own copy of
EventDispacher
.