andywer / andywer/typed-emitter

`rxjs` fromEvent compatibility

Aberta
#9 9 comentários 1 reação 0 responsáveis Ver no GitHub
enhancement
Linguagem predominante
JavaScript
Estrelas
285
Forks
29
Métricas de merge de PRs
Nenhum PR com merge em 30d

Descrição

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 https://github.com/wechaty/wechaty-redux/issues/4)

```ts
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
const emitter = new TypedEmitter()

fromEvent(emitter, 'foo')
// Error: Type 'TypedEventEmitter' 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: https://github.com/ReactiveX/rxjs/issues/4891

I try to solve this by using another great typed module [rxjs-from-emitter](https://github.com/devanshj/rxjs-from-emitter/issues/4) created by @ devanshj , however it seems that we start runing into another TypeScript bug:

```ts
type Identity = (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 https://github.com/devanshj/rxjs-from-emitter/issues/4#issuecomment-664593336 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!

Guia de contribuição

Nenhum guia de contribuição indexado para este repositório

Avaliação

Esta issue ainda não foi avaliada.

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.