Skip to content
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

Incorrect EventDispatcher’s type parameter of inherited classes #679

Open
shotamatsuda opened this issue Feb 3, 2025 · 0 comments
Open

Comments

@shotamatsuda
Copy link

shotamatsuda commented Feb 3, 2025

The types of Effect class and other classes that inherit EventDispatcher are currently defined as:

export class Effect extends EventDispatcher<import("three").Event>

However, the definitions of EventDispatcher and Event types are:

export class EventDispatcher<TEventMap extends {} = {}> {
  addEventListener<T extends Extract<keyof TEventMap, string>>(
    type: T,
    listener: EventListener<TEventMap[T], T, this>,
  ): void;

  dispatchEvent<T extends Extract<keyof TEventMap, string>>(
    event: BaseEvent<T> & TEventMap[T]
  ): void;
}

export interface Event<TEventType extends string = string, TTarget = unknown> {
  readonly type: TEventType;
  readonly target: TTarget;
}

This means Effect incorrectly expects events of “event type” "type" and "target" with the types of “event object” being string and unknown, respectively, which results in type errors when trying to call any of EventDispatcher’s functions or subclassing it.

A possible fix, without explicitly specifying the event types being used, is:

export class Effect extends EventDispatcher<Record<string, any>>

This will ensure keyof TEventMap resolves to string and TEventMap[T] to any, bypassing the type constraint issues and allowing EventDispatcher’s functions to be used without type errors.

Alternatively:

export class Effect extends EventDispatcher

This follows the default type behavior of EventDispatcher.

I suspect the type definition of EventDispatcher in @types/three was different in the past. Feel free to close this if you think it’s just a compatibility issue of @types/three.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant