andywer / typed-emitter

🔩 Type-safe event emitter interface for TypeScript
MIT License
268 stars 24 forks source link

Extending EventEmitter and implementing TypedEventEmitter is broken #43

Open orgads opened 9 months ago

orgads commented 9 months ago

Hi,

The following example works with typed-emitter v1, but breaks with v2.

import { EventEmitter } from 'events';
import TypedEmitter from 'typed-emitter';

type TestEvents = {
  foo: () => void;
};

export class ITestEventEmitter extends (EventEmitter as new () => TypedEmitter<TestEvents>) { }

export abstract class TestClass extends EventEmitter implements ITestEventEmitter {
}

Error message:

Class 'TestClass' incorrectly implements class 'ITestEventEmitter'. Did you mean to extend 'ITestEventEmitter' and inherit its members as a subclass?
  The types returned by 'rawListeners(...)' are incompatible between these types.
    Type 'Function[]' is not assignable to type 'TestEvents[E][]'.
      Type 'Function' is not assignable to type 'TestEvents[E]'.
        Type 'Function' is not assignable to type '() => void'.
          Type 'Function' provides no match for the signature '(): void'.ts(2720)

My use-case is a class that extends another class, which is an EventEmitter, but needs to also implement typed events.

orgads commented 8 months ago

ping

Tankonyako commented 7 months ago

ping

Tankonyako commented 7 months ago

i found solution,

import EventEmitter from "events";
import TypedEmitter, {EventMap} from "typed-emitter";

export default class TEventEmitter<T extends EventMap> extends (EventEmitter as { new<T extends EventMap>(): TypedEmitter<T> })<T>
{

}

then i use it by

import TEventEmitter from "@/utils/TEventEmitter";

export default class Browser extends TEventEmitter<BrowserEvents> {}
artsiommiksiuk commented 4 months ago

Unfortunately, looks like abandoned package.

artsiommiksiuk commented 4 months ago

This one works:

https://www.npmjs.com/package/tiny-typed-emitter