Skip to content

rxjs fromEvent compatibility #9

@huan

Description

@huan

Hi, @andywer

I hope you are doing well!

Recently I'm enjoying your typed-emitter module, it really nice, I love it!

After I armed all my EventEmitter with the beautiful typed-emitter, I found a compatibility problem with the fromEvent from RxJS: It does not accept our typed-emitter armed emitters.

Here's the minimum reproducible code: (related to wechaty/redux#4)

import { EventEmitter } from 'events'
import TypedEventEmitter  from 'typed-emitter'

import { fromEvent } from 'rxjs'

interface Events {
  foo: (n: number) => void
  bar: (s: string) => void
}

const TypedEmitter = EventEmitter as new () => TypedEventEmitter<Events>
const emitter = new TypedEmitter()

fromEvent(emitter, 'foo')
// Error: Type 'TypedEventEmitter<Events>' is not assignable to type 'JQueryStyleEventEmitter'.

It seems that it's due to the limitation of RxJS itself, here's some discussion about the reason that caused this issue: ReactiveX/rxjs#4891

I try to solve this by using another great typed module rxjs-from-emitter created by @ devanshj , however it seems that we start runing into another TypeScript bug:

type Identity = <T>(x: T) => T;
type Test = Identity extends (x: string) => infer T ? T : never;
// Test is expected to be `string` but is `unknown`

At last, with the help of warm-hearted @devanshj, we have created a fromTypedEvent at devanshj/rxjs-from-emitter#4 (comment) and it works like a charm.

However, because the solution requires a dependence of typed-emitter, so @devanshj does not want to include it in his rxjs-from-emitter module.

My question is: do we have a solution for this problem? If we do not, could we include this support (the code from @devanshj) to our project, so that I can use it by direct import from the module to keep us DRY, instead of creating it for every of my project?

Thank you for your great module again, and looking forward for your reply.

Have a nice day!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions