andywer / typed-emitter

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

Updating to v2.0.0 #28

Open leumasme opened 2 years ago

leumasme commented 2 years ago

Hey! I just installed typed-emitter v2.0.0 and am not sure how to use it now. The code that worked previously and that still follows what is in the readme,

import EventEmitter from "events"
import TypedEmitter from "typed-emitter"

interface MyEvents {
    "finish": (results: any[]) => void
}

export class MyClass extends (EventEmitter as new () => TypedEmitter<MyEvents>) {
}

now errors on EventEmitter as new () => TypedEmitter<MyEvents>

Conversion of type 'typeof EventEmitter' to type 'new () => TypedEventEmitter<MyEvents>' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
  Types of construct signatures are incompatible.
    Type 'new (options?: EventEmitterOptions) => EventEmitter' is not assignable to type 'new () => TypedEventEmitter<MyEvents>'.
      Property '__events' is missing in type 'EventEmitter' but required in type 'TypedEventEmitter<MyEvents>'.ts(2352)
index.d.ts(46, 3): '__events' is declared here.

Downgrading to typed-emitter@^1 and it works again, so how do i get this to work on v2? Thanks!

andywer commented 2 years ago

Hey @leumasme.

You need to use a type MyEvents =, not interface MyEvents. See #27. The readme has been updated, a new release with updated jsdoc has not yet been published yet, though.

Should we add a prominent note to the readme?