andywer / typed-emitter

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

Document how to extend an emitter #8

Closed andywer closed 4 years ago

yoshigev commented 4 years ago

Thanks. That was exactly what I meant.

erfanium commented 4 years ago

And also creating generic class is helpful:

class MyEventEmitter<T> extends (EventEmitter as { new<T>(): TypedEmitter<T> })<T> 
erfanium commented 4 years ago

@andywer Thanks! But both you and I forgot to leave the class braces!😅

miracle2k commented 4 years ago

I am getting this error:

Conversion of type 'typeof EventEmitter' to type 'new () => TypedEventEmitter<TableEvents>' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
  Type 'typeof EventEmitter' provides no match for the signature 'new (): TypedEventEmitter<TableEvents>'.
    "events": "^3.2.0",
    "typescript": "^3.9.7"
    "typed-emitter": "^1.3.0"

Is there a particular events package I should use?

erfanium commented 4 years ago

@miracle2k You can fix this by casting default EventEmitter type to unknown This should work (But I didn't test it)

class MyEventEmitter<T> extends (EventEmitter as unknown as { new<T>(): TypedEmitter<T> })<T> {
  // ...
}
miracle2k commented 4 years ago

@erfanium Indeed, and I ended up doing that. Just wondering if people were doing something more cleaner, given that it wasn't mentioned in the README and related discussions.

erfanium commented 4 years ago

@erfanium May events module types from npm be different from node's native event module types, Which has caused this problem