Skip to content

Commit

Permalink
Fix __events prop breaking inheritance (#31)
Browse files Browse the repository at this point in the history
* Add rxjs as dev dependency

To get rid of the error in the IDE.

* Move `__events` hack into separate interface for rxjs
  • Loading branch information
andywer authored Jan 22, 2022
1 parent 30f7534 commit 0d84add
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ This can be fixed by the following code, by replacing the `fromEvent` type with

```ts
import { fromEvent as rxFromEvent } from "rxjs"
import { FromEvent } from "typed-emitter/rxjs"
import { default as TypedEmitter, FromEvent } from "typed-emitter/rxjs"

// The `Observable` typing can be correctly inferenced
const fromEvent = rxFromEvent as FromEvent
Expand Down
8 changes: 1 addition & 7 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type EventMap = {
export type EventMap = {
[key: string]: (...args: any[]) => void
}

Expand Down Expand Up @@ -38,12 +38,6 @@ interface TypedEventEmitter<Events extends EventMap> {

getMaxListeners (): number
setMaxListeners (maxListeners: number): this

/**
* required by `FromEvent`
* @see https://github.com/devanshj/rxjs-from-emitter/issues/4#issuecomment-665104646
*/
__events: Events
}

export default TypedEventEmitter
48 changes: 47 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
"types": "./index.d.ts",
"optionalDependencies": {
"rxjs": "*"
},
"devDependencies": {
"rxjs": "^7.5.2"
}
}
24 changes: 17 additions & 7 deletions rxjs/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
/**
* devanshj/rxjs-from-emitter#4 - typed-emitter compatibility
*
*
* @see https://github.com/devanshj/rxjs-from-emitter/issues/4#issuecomment-665104646
*/
/* eslint-disable no-use-before-define */
import {
fromEvent as rxFromEvent,
import {
fromEvent as rxFromEvent,
Observable,
} from 'rxjs'
import type TypedEventEmitter from '../index'
import type { default as BaseTypedEmitter, EventMap } from '../index'

type ObservedValue<A extends unknown[]> =
A['length'] extends 0 ? void :
A['length'] extends 1 ? A[0] :
A

interface FromTypedEvent {
< Emitter extends TypedEventEmitter<any>
< Emitter extends TypedEmitter<any>
, EventName extends keyof Events
, Events = Emitter extends TypedEventEmitter<infer T> ? T : never
>(emitter: Emitter, event: EventName): Observable<ObservedValue<Events[EventName] extends (...args: infer A) => any ? A : never>>
, Events = Emitter extends TypedEmitter<infer T> ? T : never
>(emitter: Emitter, event: EventName): Observable<ObservedValue<Events[EventName] extends (...args: infer A) => any ? A : never>>
}

export type FromEvent = FromTypedEvent & typeof rxFromEvent

interface TypedEmitter<Events extends EventMap> extends BaseTypedEmitter<Events> {
/**
* required by `FromEvent`
* @see https://github.com/devanshj/rxjs-from-emitter/issues/4#issuecomment-665104646
*/
__events: Events
}

export default TypedEmitter

0 comments on commit 0d84add

Please sign in to comment.