diff --git a/types/three/examples/jsm/controls/ArcballControls.d.ts b/types/three/examples/jsm/controls/ArcballControls.d.ts index 066f31126..2b4b976a8 100644 --- a/types/three/examples/jsm/controls/ArcballControls.d.ts +++ b/types/three/examples/jsm/controls/ArcballControls.d.ts @@ -14,7 +14,13 @@ export enum ArcballControlsMouseActionKeys { CTRL = 'CTRL', } -export class ArcballControls extends EventDispatcher { +export interface ArcballControlsEventMap { + change: {}; + start: {}; + end: {}; +} + +export class ArcballControls extends EventDispatcher { camera: Camera | null; domElement: HTMLElement; scene?: Scene | null | undefined; diff --git a/types/three/examples/jsm/controls/DragControls.d.ts b/types/three/examples/jsm/controls/DragControls.d.ts index 49fddff3a..31847e362 100644 --- a/types/three/examples/jsm/controls/DragControls.d.ts +++ b/types/three/examples/jsm/controls/DragControls.d.ts @@ -1,6 +1,14 @@ import { Camera, EventDispatcher, Object3D, Raycaster } from '../../../src/Three.js'; -export class DragControls extends EventDispatcher { +export interface DragControlsEventMap { + hoveron: { object: Object3D }; + hoveroff: { object: Object3D }; + dragstart: { object: Object3D }; + drag: { object: Object3D }; + dragend: { object: Object3D }; +} + +export class DragControls extends EventDispatcher { constructor(objects: Object3D[], camera: Camera, domElement?: HTMLElement); object: Camera; diff --git a/types/three/examples/jsm/controls/FlyControls.d.ts b/types/three/examples/jsm/controls/FlyControls.d.ts index f1402824d..a0165406b 100644 --- a/types/three/examples/jsm/controls/FlyControls.d.ts +++ b/types/three/examples/jsm/controls/FlyControls.d.ts @@ -1,6 +1,10 @@ import { Camera, EventDispatcher } from '../../../src/Three.js'; -export class FlyControls extends EventDispatcher { +export interface FlyControlsEventMap { + change: {}; +} + +export class FlyControls extends EventDispatcher { constructor(object: Camera, domElement?: HTMLElement); autoForward: boolean; diff --git a/types/three/examples/jsm/controls/OrbitControls.d.ts b/types/three/examples/jsm/controls/OrbitControls.d.ts index ceee21991..4bb4b4142 100644 --- a/types/three/examples/jsm/controls/OrbitControls.d.ts +++ b/types/three/examples/jsm/controls/OrbitControls.d.ts @@ -1,4 +1,10 @@ -import { Camera, MOUSE, TOUCH, Vector3 } from '../../../src/Three.js'; +import { Camera, EventDispatcher, MOUSE, TOUCH, Vector3 } from '../../../src/Three.js'; + +export interface OrbitControlsEventMap { + change: {}; + start: {}; + end: {}; +} /** * Orbit controls allow the camera to orbit around a target. @@ -7,7 +13,7 @@ import { Camera, MOUSE, TOUCH, Vector3 } from '../../../src/Three.js'; * @param domElement - The HTML element used for * event listeners. */ -export class OrbitControls { +export class OrbitControls extends EventDispatcher { constructor(object: Camera, domElement?: HTMLElement); /** @@ -272,13 +278,4 @@ export class OrbitControls { * Returns the distance from the camera to the target. */ getDistance(): number; - - // EventDispatcher mixins - addEventListener(type: string, listener: (event: any) => void): void; - - hasEventListener(type: string, listener: (event: any) => void): boolean; - - removeEventListener(type: string, listener: (event: any) => void): void; - - dispatchEvent(event: { type: string; target: any }): void; } diff --git a/types/three/examples/jsm/controls/TrackballControls.d.ts b/types/three/examples/jsm/controls/TrackballControls.d.ts index 7bacaad20..1469d152e 100644 --- a/types/three/examples/jsm/controls/TrackballControls.d.ts +++ b/types/three/examples/jsm/controls/TrackballControls.d.ts @@ -1,6 +1,12 @@ import { Camera, EventDispatcher, MOUSE, Vector3 } from '../../../src/Three.js'; -export class TrackballControls extends EventDispatcher { +export interface TrackballControlsEventMap { + change: {}; + start: {}; + end: {}; +} + +export class TrackballControls extends EventDispatcher { constructor(object: Camera, domElement?: HTMLElement); object: Camera; diff --git a/types/three/examples/jsm/controls/TransformControls.d.ts b/types/three/examples/jsm/controls/TransformControls.d.ts index 45d6c46f0..5e94e1f6a 100644 --- a/types/three/examples/jsm/controls/TransformControls.d.ts +++ b/types/three/examples/jsm/controls/TransformControls.d.ts @@ -1,6 +1,38 @@ -import { Object3D, Camera, MOUSE, Raycaster, Mesh, Vector3, Quaternion } from '../../../src/Three.js'; +import { Object3D, Camera, MOUSE, Raycaster, Mesh, Vector3, Quaternion, Object3DEventMap } from '../../../src/Three.js'; -export class TransformControls extends Object3D { +export interface TransformControlsEventMap extends Object3DEventMap { + change: {}; + mouseDown: {}; + mouseUp: {}; + objectChange: {}; + 'camera-changed': { value: unknown }; + 'object-changed': { value: unknown }; + 'enabled-changed': { value: unknown }; + 'axis-changed': { value: unknown }; + 'mode-changed': { value: unknown }; + 'translationSnap-changed': { value: unknown }; + 'rotationSnap-changed': { value: unknown }; + 'scaleSnap-changed': { value: unknown }; + 'space-changed': { value: unknown }; + 'size-changed': { value: unknown }; + 'dragging-changed': { value: unknown }; + 'showX-changed': { value: unknown }; + 'showY-changed': { value: unknown }; + 'showZ-changed': { value: unknown }; + 'worldPosition-changed': { value: unknown }; + 'worldPositionStart-changed': { value: unknown }; + 'worldQuaternion-changed': { value: unknown }; + 'worldQuaternionStart-changed': { value: unknown }; + 'cameraPosition-changed': { value: unknown }; + 'cameraQuaternion-changed': { value: unknown }; + 'pointStart-changed': { value: unknown }; + 'pointEnd-changed': { value: unknown }; + 'rotationAxis-changed': { value: unknown }; + 'rotationAngle-changed': { value: unknown }; + 'eye-changed': { value: unknown }; +} + +export class TransformControls extends Object3D { constructor(object: Camera, domElement?: HTMLElement); domElement: HTMLElement; @@ -20,6 +52,7 @@ export class TransformControls extends Object3D { showX: boolean; showY: boolean; showZ: boolean; + readonly isTransformControls: true; mouseButtons: { LEFT: MOUSE; MIDDLE: MOUSE; RIGHT: MOUSE }; diff --git a/types/three/examples/jsm/interactive/InteractiveGroup.d.ts b/types/three/examples/jsm/interactive/InteractiveGroup.d.ts index 06934f128..5a92f14ce 100644 --- a/types/three/examples/jsm/interactive/InteractiveGroup.d.ts +++ b/types/three/examples/jsm/interactive/InteractiveGroup.d.ts @@ -1,4 +1,17 @@ -import { WebGLRenderer, Camera, Group } from 'three'; +import { Camera, Group, Object3D, Object3DEventMap, Vector2, WebGLRenderer } from '../../../src/Three.js'; + +export interface InteractiveObject3DEventMap extends Object3DEventMap { + hoveron: { data: Vector2 }; + pointerdown: { data: Vector2 }; + pointerup: { data: Vector2 }; + pointermove: { data: Vector2 }; + mousedown: { data: Vector2 }; + mouseup: { data: Vector2 }; + mousemove: { data: Vector2 }; + click: { data: Vector2 }; +} + +export class InteractiveObject3D extends Object3D {} export class InteractiveGroup extends Group { constructor(renderer: WebGLRenderer, camera: Camera); diff --git a/types/three/examples/jsm/webxr/XREstimatedLight.d.ts b/types/three/examples/jsm/webxr/XREstimatedLight.d.ts index 10db5244d..593563fc8 100644 --- a/types/three/examples/jsm/webxr/XREstimatedLight.d.ts +++ b/types/three/examples/jsm/webxr/XREstimatedLight.d.ts @@ -1,4 +1,4 @@ -import { DirectionalLight, Group, LightProbe, Texture, WebGLRenderer } from '../../../src/Three.js'; +import { DirectionalLight, Group, LightProbe, Texture, WebGLRenderer, Object3DEventMap } from '../../../src/Three.js'; export class SessionLightProbe { xrLight: XREstimatedLight; @@ -23,7 +23,12 @@ export class SessionLightProbe { dispose: () => void; } -export class XREstimatedLight extends Group { +export interface XREstimatedLightEventMap extends Object3DEventMap { + estimationstart: {}; + estimationend: {}; +} + +export class XREstimatedLight extends Group { lightProbe: LightProbe; directionalLight: DirectionalLight; environment: Texture; diff --git a/types/three/src/animation/AnimationMixer.d.ts b/types/three/src/animation/AnimationMixer.d.ts index bf986370e..2b2295c87 100644 --- a/types/three/src/animation/AnimationMixer.d.ts +++ b/types/three/src/animation/AnimationMixer.d.ts @@ -5,7 +5,12 @@ import { EventDispatcher } from './../core/EventDispatcher.js'; import { Object3D } from '../core/Object3D.js'; import { AnimationObjectGroup } from './AnimationObjectGroup.js'; -export class AnimationMixer extends EventDispatcher { +export interface AnimationMixerEventMap { + loop: { action: AnimationAction; loopDelta: number }; + finished: { action: AnimationAction; direction: number }; +} + +export class AnimationMixer extends EventDispatcher { constructor(root: Object3D | AnimationObjectGroup); /** diff --git a/types/three/src/core/BufferGeometry.d.ts b/types/three/src/core/BufferGeometry.d.ts index 449aeb0f8..ecab628bc 100644 --- a/types/three/src/core/BufferGeometry.d.ts +++ b/types/three/src/core/BufferGeometry.d.ts @@ -75,7 +75,7 @@ export type NormalOrGLBufferAttributes = Record< */ export class BufferGeometry< Attributes extends NormalOrGLBufferAttributes = NormalBufferAttributes, -> extends EventDispatcher { +> extends EventDispatcher<{ dispose: {} }> { /** * This creates a new {@link THREE.BufferGeometry | BufferGeometry} object. */ diff --git a/types/three/src/core/EventDispatcher.d.ts b/types/three/src/core/EventDispatcher.d.ts index f115baaab..fa8048a96 100644 --- a/types/three/src/core/EventDispatcher.d.ts +++ b/types/three/src/core/EventDispatcher.d.ts @@ -1,15 +1,21 @@ -export interface BaseEvent { - type: string; +/** + * The minimal basic Event that can be dispatched by a {@link EventDispatcher<>}. + */ +export interface BaseEvent { + readonly type: TEventType; } /** - * Event object. + * The minimal expected contract of a fired Event that was dispatched by a {@link EventDispatcher<>}. */ -export interface Event extends BaseEvent { - target?: any; - [attachment: string]: any; +export interface Event { + readonly type: TEventType; + readonly target: TTarget; } -export type EventListener = (event: E & { type: T } & { target: U }) => void; + +export type EventListener = ( + event: TEventData & Event, +) => void; /** * JavaScript events for custom objects @@ -32,7 +38,7 @@ export type EventListener = (event: E & { type: T } & { target: U }) => * @see {@link https://threejs.org/docs/index.html#api/en/core/EventDispatcher | Official Documentation} * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/EventDispatcher.js | Source} */ -export class EventDispatcher { +export class EventDispatcher { /** * Creates {@link THREE.EventDispatcher | EventDispatcher} object. */ @@ -43,25 +49,37 @@ export class EventDispatcher { * @param type The type of event to listen to. * @param listener The function that gets called when the event is fired. */ - addEventListener(type: T, listener: EventListener): void; + addEventListener>( + type: T, + listener: EventListener, + ): void; + addEventListener(type: T, listener: EventListener<{}, T, this>): void; /** * Checks if listener is added to an event type. * @param type The type of event to listen to. * @param listener The function that gets called when the event is fired. */ - hasEventListener(type: T, listener: EventListener): boolean; + hasEventListener>( + type: T, + listener: EventListener, + ): boolean; + hasEventListener(type: T, listener: EventListener<{}, T, this>): boolean; /** * Removes a listener from an event type. * @param type The type of the listener that gets removed. * @param listener The listener function that gets removed. */ - removeEventListener(type: T, listener: EventListener): void; + removeEventListener>( + type: T, + listener: EventListener, + ): void; + removeEventListener(type: T, listener: EventListener<{}, T, this>): void; /** * Fire an event type. * @param event The event that gets fired. */ - dispatchEvent(event: E): void; + dispatchEvent>(event: BaseEvent & TEventMap[T]): void; } diff --git a/types/three/src/core/Object3D.d.ts b/types/three/src/core/Object3D.d.ts index dce3c5478..42f70a1ba 100644 --- a/types/three/src/core/Object3D.d.ts +++ b/types/three/src/core/Object3D.d.ts @@ -1,19 +1,24 @@ -import { Vector3 } from './../math/Vector3.js'; -import { Euler } from './../math/Euler.js'; -import { Quaternion } from './../math/Quaternion.js'; -import { Matrix4 } from './../math/Matrix4.js'; -import { Matrix3 } from './../math/Matrix3.js'; +import { Vector3 } from '../math/Vector3.js'; +import { Euler } from '../math/Euler.js'; +import { Quaternion } from '../math/Quaternion.js'; +import { Matrix4 } from '../math/Matrix4.js'; +import { Matrix3 } from '../math/Matrix3.js'; import { Layers } from './Layers.js'; -import { WebGLRenderer } from './../renderers/WebGLRenderer.js'; -import { Scene } from './../scenes/Scene.js'; -import { Camera } from './../cameras/Camera.js'; -import { Material } from './../materials/Material.js'; -import { Group } from './../objects/Group.js'; +import { WebGLRenderer } from '../renderers/WebGLRenderer.js'; +import { Scene } from '../scenes/Scene.js'; +import { Camera } from '../cameras/Camera.js'; +import { Material } from '../materials/Material.js'; +import { Group } from '../objects/Group.js'; import { Intersection, Raycaster } from './Raycaster.js'; -import { EventDispatcher, BaseEvent, Event } from './EventDispatcher.js'; +import { EventDispatcher } from './EventDispatcher.js'; import { BufferGeometry } from './BufferGeometry.js'; import { AnimationClip } from '../animation/AnimationClip.js'; +export interface Object3DEventMap { + added: {}; + removed: {}; +} + /** * This is the base class for most objects in three.js and provides a set of properties and methods for manipulating objects in 3D space. * @remarks Note that this can be used for grouping objects via the {@link THREE.Object3D.add | .add()} method which adds the object as a child, @@ -21,7 +26,7 @@ import { AnimationClip } from '../animation/AnimationClip.js'; * @see {@link https://threejs.org/docs/index.html#api/en/core/Object3D | Official Documentation} * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/Object3D.js | Source} */ -export class Object3D extends EventDispatcher { +export class Object3D extends EventDispatcher { /** * This creates a new {@link Object3D} object. */ diff --git a/types/three/src/core/RenderTarget.d.ts b/types/three/src/core/RenderTarget.d.ts index fa960ad92..8d43beaa0 100644 --- a/types/three/src/core/RenderTarget.d.ts +++ b/types/three/src/core/RenderTarget.d.ts @@ -33,7 +33,7 @@ export interface RenderTargetOptions { encoding?: TextureEncoding | undefined; } -export class RenderTarget extends EventDispatcher { +export class RenderTarget extends EventDispatcher<{ dispose: {} }> { constructor(width?: number, height?: number, options?: RenderTargetOptions); readonly isRenderTarget: true; diff --git a/types/three/src/core/UniformsGroup.d.ts b/types/three/src/core/UniformsGroup.d.ts index 736b3a3bf..4df29ac4c 100644 --- a/types/three/src/core/UniformsGroup.d.ts +++ b/types/three/src/core/UniformsGroup.d.ts @@ -6,7 +6,7 @@ import { Usage } from '../constants.js'; * @see Example: {@link https://threejs.org/examples/#webgl2_ubo | WebGL2 / UBO} * @see {@link https://github.com/mrdoob/three.js/blob/master/src/core/UniformsGroup.js | Source} */ -export class UniformsGroup extends EventDispatcher { +export class UniformsGroup extends EventDispatcher<{ dispose: {} }> { constructor(); readonly isUniformsGroup: true; diff --git a/types/three/src/materials/Material.d.ts b/types/three/src/materials/Material.d.ts index ef580be64..0846c1984 100644 --- a/types/three/src/materials/Material.d.ts +++ b/types/three/src/materials/Material.d.ts @@ -63,7 +63,7 @@ export interface MaterialParameters { /** * Materials describe the appearance of objects. They are defined in a (mostly) renderer-independent way, so you don't have to rewrite materials if you decide to use a different renderer. */ -export class Material extends EventDispatcher { +export class Material extends EventDispatcher<{ dispose: {} }> { constructor(); alphaHash: boolean; diff --git a/types/three/src/objects/Group.d.ts b/types/three/src/objects/Group.d.ts index 7f2db352a..fa18c3cb3 100644 --- a/types/three/src/objects/Group.d.ts +++ b/types/three/src/objects/Group.d.ts @@ -1,4 +1,4 @@ -import { Object3D } from './../core/Object3D.js'; +import { Object3D, Object3DEventMap } from '../core/Object3D.js'; /** * Its purpose is to make working with groups of objects syntactically clearer. @@ -22,7 +22,7 @@ import { Object3D } from './../core/Object3D.js'; * @see {@link https://threejs.org/docs/index.html#api/en/objects/Group | Official Documentation} * @see {@link https://github.com/mrdoob/three.js/blob/master/src/objects/Group.js | Source} */ -export class Group extends Object3D { +export class Group extends Object3D { /** * Creates a new {@link Group}. */ diff --git a/types/three/src/objects/InstancedMesh.d.ts b/types/three/src/objects/InstancedMesh.d.ts index cb9fb426a..634577a47 100644 --- a/types/three/src/objects/InstancedMesh.d.ts +++ b/types/three/src/objects/InstancedMesh.d.ts @@ -1,11 +1,16 @@ import { BufferGeometry } from '../core/BufferGeometry.js'; -import { Material } from './../materials/Material.js'; -import { BufferAttribute } from './../core/BufferAttribute.js'; +import { Material } from '../materials/Material.js'; import { InstancedBufferAttribute } from '../core/InstancedBufferAttribute.js'; import { Mesh } from './Mesh.js'; -import { Matrix4 } from './../math/Matrix4.js'; -import { Color } from './../math/Color.js'; -import { Box3, Sphere } from '../Three.js'; +import { Matrix4 } from '../math/Matrix4.js'; +import { Color } from '../math/Color.js'; +import { Object3DEventMap } from '../core/Object3D.js'; +import { Box3 } from '../math/Box3.js'; +import { Sphere } from '../math/Sphere.js'; + +export interface InstancedMeshEventMap extends Object3DEventMap { + dispose: {}; +} /** * A special version of {@link THREE.Mesh | Mesh} with instanced rendering support @@ -23,7 +28,7 @@ import { Box3, Sphere } from '../Three.js'; export class InstancedMesh< TGeometry extends BufferGeometry = BufferGeometry, TMaterial extends Material | Material[] = Material | Material[], -> extends Mesh { +> extends Mesh { /** * Create a new instance of {@link InstancedMesh} * @param geometry An instance of {@link THREE.BufferGeometry | BufferGeometry}. diff --git a/types/three/src/objects/Mesh.d.ts b/types/three/src/objects/Mesh.d.ts index 76795f581..62651415f 100644 --- a/types/three/src/objects/Mesh.d.ts +++ b/types/three/src/objects/Mesh.d.ts @@ -1,8 +1,6 @@ -import { Material } from './../materials/Material.js'; -import { Raycaster } from './../core/Raycaster.js'; -import { Object3D } from './../core/Object3D.js'; +import { Material } from '../materials/Material.js'; +import { Object3D, Object3DEventMap } from '../core/Object3D.js'; import { BufferGeometry } from '../core/BufferGeometry.js'; -import { Intersection } from '../core/Raycaster.js'; import { Vector3 } from '../math/Vector3.js'; /** @@ -24,7 +22,8 @@ import { Vector3 } from '../math/Vector3.js'; export class Mesh< TGeometry extends BufferGeometry = BufferGeometry, TMaterial extends Material | Material[] = Material | Material[], -> extends Object3D { + TEventMap extends Object3DEventMap = Object3DEventMap, +> extends Object3D { /** * Create a new instance of {@link Mesh} * @param geometry An instance of {@link THREE.BufferGeometry | BufferGeometry}. Default {@link THREE.BufferGeometry | `new THREE.BufferGeometry()`}. diff --git a/types/three/src/renderers/webxr/WebXRController.d.ts b/types/three/src/renderers/webxr/WebXRController.d.ts index ce858507c..624ca1a00 100644 --- a/types/three/src/renderers/webxr/WebXRController.d.ts +++ b/types/three/src/renderers/webxr/WebXRController.d.ts @@ -1,5 +1,6 @@ import { Group } from '../../objects/Group.js'; import { Vector3 } from '../../math/Vector3.js'; +import { Object3DEventMap } from '../../core/Object3D.js'; export type XRControllerEventType = XRSessionEventType | XRInputSourceEventType | 'disconnected' | 'connected'; @@ -13,19 +14,36 @@ export interface XRHandInputState { pinching: boolean; } -export class XRHandSpace extends Group { +export interface WebXRSpaceEventMap extends Object3DEventMap { + select: { data: XRInputSource }; + selectstart: { data: XRInputSource }; + selectend: { data: XRInputSource }; + squeeze: { data: XRInputSource }; + squeezestart: { data: XRInputSource }; + squeezeend: { data: XRInputSource }; + + connected: { data: XRInputSource }; + disconnected: { data: XRInputSource }; + + pinchend: { handedness: XRHandedness; target: WebXRController }; // This Event break the THREE.EventDispatcher contract, replacing the target to the wrong instance. + pinchstart: { handedness: XRHandedness; target: WebXRController }; // This Event break the THREE.EventDispatcher contract, replacing the target to the wrong instance. + + move: {}; +} + +export class XRHandSpace extends Group { readonly joints: Partial; readonly inputState: XRHandInputState; } -export class XRTargetRaySpace extends Group { +export class XRTargetRaySpace extends Group { hasLinearVelocity: boolean; readonly linearVelocity: Vector3; hasAngularVelocity: boolean; readonly angularVelocity: Vector3; } -export class XRGripSpace extends Group { +export class XRGripSpace extends Group { hasLinearVelocity: boolean; readonly linearVelocity: Vector3; hasAngularVelocity: boolean; diff --git a/types/three/src/renderers/webxr/WebXRManager.d.ts b/types/three/src/renderers/webxr/WebXRManager.d.ts index bf733cc2d..a998c91c9 100644 --- a/types/three/src/renderers/webxr/WebXRManager.d.ts +++ b/types/three/src/renderers/webxr/WebXRManager.d.ts @@ -11,7 +11,16 @@ import { XRTargetRaySpace, XRGripSpace, XRHandSpace } from './WebXRController.js export type WebXRCamera = PerspectiveCamera & { viewport: Vector4 }; export type WebXRArrayCamera = Omit & { cameras: [WebXRCamera, WebXRCamera] }; -export class WebXRManager extends EventDispatcher { +export interface WebXRManagerEventMap { + sessionstart: {}; + sessionend: {}; + planeadded: { data: XRPlane }; + planeremoved: { data: XRPlane }; + planechanged: { data: XRPlane }; + planesdetected: { data: XRPlaneSet }; +} + +export class WebXRManager extends EventDispatcher { constructor(renderer: any, gl: WebGLRenderingContext); /** diff --git a/types/three/src/textures/Texture.d.ts b/types/three/src/textures/Texture.d.ts index ca658cd2b..98115d631 100644 --- a/types/three/src/textures/Texture.d.ts +++ b/types/three/src/textures/Texture.d.ts @@ -1,7 +1,7 @@ -import { Vector2 } from './../math/Vector2.js'; -import { Matrix3 } from './../math/Matrix3.js'; +import { Vector2 } from '../math/Vector2.js'; +import { Matrix3 } from '../math/Matrix3.js'; import { Source } from './Source.js'; -import { EventDispatcher } from './../core/EventDispatcher.js'; +import { EventDispatcher } from '../core/EventDispatcher.js'; import { Mapping, Wrapping, @@ -38,7 +38,7 @@ export interface OffscreenCanvas extends EventTarget {} * @see {@link https://threejs.org/docs/index.html#api/en/textures/Texture | Official Documentation} * @see {@link https://github.com/mrdoob/three.js/blob/master/src/Textures/Texture.js | Source} */ -export class Texture extends EventDispatcher { +export class Texture extends EventDispatcher<{ dispose: {} }> { /** * This creates a new {@link THREE.Texture | Texture} object. * @param image See {@link Texture.image | .image}. Default {@link THREE.Texture.DEFAULT_IMAGE} diff --git a/types/three/test/integration/three-examples/webxr_vr_handinput_cubes.ts b/types/three/test/integration/three-examples/webxr_vr_handinput_cubes.ts index 9b6555dd6..0b26fcb90 100644 --- a/types/three/test/integration/three-examples/webxr_vr_handinput_cubes.ts +++ b/types/three/test/integration/three-examples/webxr_vr_handinput_cubes.ts @@ -153,7 +153,7 @@ function onWindowResize() { } const SphereRadius = 0.05; -function onPinchStartLeft(event: THREE.Event) { +function onPinchStartLeft(event: THREE.Event<'pinchstart', Hand>) { const controller = event.target; if (grabbing) { @@ -203,8 +203,8 @@ function collideObject(indexTip: THREE.Group) { return null; } -function onPinchStartRight(event: THREE.Event) { - const controller = event.target as Hand; +function onPinchStartRight(event: THREE.Event<'pinchstart', Hand>) { + const controller = event.target; const indexTip = controller.joints['index-finger-tip']; const object = collideObject(indexTip); if (object) { @@ -215,7 +215,7 @@ function onPinchStartRight(event: THREE.Event) { } } -function onPinchEndRight(event: THREE.Event) { +function onPinchEndRight(event: THREE.Event<'pinchend', Hand>) { const controller = event.target; if (controller.userData.selected !== undefined) { diff --git a/types/three/test/unit/src/core/EventDispatcher.ts b/types/three/test/unit/src/core/EventDispatcher.ts index d249a1c64..2811a39aa 100644 --- a/types/three/test/unit/src/core/EventDispatcher.ts +++ b/types/three/test/unit/src/core/EventDispatcher.ts @@ -1,44 +1,88 @@ import * as THREE from 'three'; // Test for legacy usage -const eveDisForAnyEvent = new THREE.EventDispatcher(); +const eveDisForAnyEvent = new THREE.EventDispatcher>(); eveDisForAnyEvent.addEventListener('eventA', e => { e.type; // $ExpectType "eventA" - e.target; // $ExpectType any + e.target; // $ExpectType EventDispatcher> + // @ts-expect-error e.bar(); }); eveDisForAnyEvent.dispatchEvent({ type: 'eventA' }); +eveDisForAnyEvent.dispatchEvent({ type: 'eventB', otherProp: 42 }); + eveDisForAnyEvent.removeEventListener('eventA', e => { e.type; // $ExpectType "eventA" - e.target; // $ExpectType any + e.target; // $ExpectType EventDispatcher> }); eveDisForAnyEvent.hasEventListener('eventA', e => { e.type; // $ExpectType "eventA" - e.target; // $ExpectType any + e.target; // $ExpectType EventDispatcher> }); // Test for typed events -type TestEvent = { type: 'foo'; foo: number } | { type: 'bar'; bar: string }; +interface TestEvent { + foo: { foo: number }; + bar: { bar: string }; +} + const eveDisForTestEvent = new THREE.EventDispatcher(); eveDisForTestEvent.addEventListener('foo', e => { e.type; // $ExpectType "foo" e.target; // $ExpectType EventDispatcher + e.foo; // $ExpectType number // NOTE: Error in ts lower than 3.9. The minimum typescript version cannot be raised from 3.6 because of the dependency from aframe. // e.foo; // $ExpectType number // @ts-expect-error e.bar; }); -// call addEventListener with an invalid event -// @ts-expect-error -eveDisForTestEvent.addEventListener('baz', () => {}); +eveDisForTestEvent.addEventListener('bar', e => { + e.type; // $ExpectType "bar" + e.target; // $ExpectType EventDispatcher + e.bar; // $ExpectType string + // @ts-expect-error + e.foo; +}); + +// call addEventListener with an unknown event. The typing should allow you listen any unknown event. +eveDisForTestEvent.addEventListener('baz', e => { + e.type; // $ExpectType "baz" + e.target; // $ExpectType EventDispatcher + // @ts-expect-error + e.bar; + // @ts-expect-error + e.foo; + // @ts-expect-error + e.bar(); +}); +eveDisForTestEvent.addEventListener('NotRegistered', e => { + e.type; // $ExpectType "NotRegistered" + e.target; // $ExpectType EventDispatcher + // @ts-expect-error + e.bar; + // @ts-expect-error + e.foo; + // @ts-expect-error + e.bar(); +}); eveDisForTestEvent.dispatchEvent({ type: 'foo', foo: 42 }); +eveDisForTestEvent.dispatchEvent({ type: 'bar', bar: '42' }); +// @ts-expect-error +eveDisForTestEvent.dispatchEvent({ type: 'zzzz', shouldWork: '42' }); +// @ts-expect-error +eveDisForTestEvent.dispatchEvent({ type: 'eventA' }); + // call dispatchEvent with an invalid event // @ts-expect-error eveDisForTestEvent.dispatchEvent({ type: 'foo', foo: '42' }); // @ts-expect-error eveDisForTestEvent.dispatchEvent({ type: 'foo', bar: '42' }); +// @ts-expect-error +eveDisForTestEvent.dispatchEvent({ type: 'bar', bar: 42 }); +// @ts-expect-error +eveDisForTestEvent.dispatchEvent({ bar: 42 }); eveDisForTestEvent.removeEventListener('bar', () => {}); eveDisForTestEvent.hasEventListener('bar', () => {});