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

feat(fromEvent): add FromEvent type export #19

Merged
merged 5 commits into from
Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ class MyEventEmitter<T> extends (EventEmitter as { new<T>(): TypedEmitter<T> })<
}
```

## RxJS `fromEvent` types inference

The default `fromEvent` from RxJS will return an `Observable<unknown>` for our typed emitter.

This can be fixed by the following code, by replacing the `fromEvent` type with our enhanced one: `FromEvent`:

```ts
import { fromEvent as rxjsFromEvent } from "rxjs"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really tiny suggestion: rxjsFromEvent is quite a handful. What would you think about renaming the import alias to rxFromEvent?

It's just two letters difference, but I think it would make the code feel "friendlier".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely agree with this.

Have renamed rxjsFromEvent to rxFromEvent from both the README and the source code.

import { FromEvent } from "typed-emitter/rxjs"

// The `Observable` typing can be correctly inferenced
const fromEvent = rxjsFromEvent as FromEvent
```

Learn more from [rxjs fromEvent compatibility #9](https://github.com/andywer/typed-emitter/issues/9)
for the `fromEvent` compatibility discussions.

## Why another package?

The interface that comes with `@types/node` is not type-safe at all. It does not even offer a way of specifying the events that the emitter will emit...
Expand Down
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ interface TypedEventEmitter<Events> {

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
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
"interface"
],
"main": "./types.js",
"types": "./index.d.ts"
"types": "./index.d.ts",
"optionalDependencies": {
"rxjs": "*"
}
}
25 changes: 25 additions & 0 deletions rxjs/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* 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 rxjsFromEvent,
Observable,
} from 'rxjs'
import type TypedEventEmitter from 'typed-emitter'

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

interface FromTypedEvent {
< Emitter extends TypedEventEmitter<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>>
}

export type FromEvent = FromTypedEvent & typeof rxjsFromEvent
1 change: 1 addition & 0 deletions rxjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// https://github.com/octokit/openapi-types.ts/issues/16#issuecomment-772784156