baconjs / bacon.js

Functional reactive programming library for TypeScript and JavaScript

Home Page:https://baconjs.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bus.plug type ergonomics

joux3 opened this issue · comments

Hi,

I think Bus.plug could be more ergonomic. plug seems to require type parameter for the stream being plugged into the Bus in some cases. It seems that Bus<A>.plug requires the plugged stream type B to be assignable to A and A to be assignable to B while I believe that the former would be enough. (Note: strict mode TS is required for the compile error)

import * as Bacon from "baconjs";

type TestObject = { a?: number; b?: string };
const testBus = new Bacon.Bus<TestObject>();

// these work
testBus.push({ a: 242124 });
testBus.push({ b: "jee" });

// this works:
testBus.plug(Bacon.interval(1000, 0).map<TestObject>(() => ({ a: 999 })));

// this does not compile
testBus.plug(Bacon.interval(1000, 0).map(() => ({ a: 999 })));

// type testing  done by @milankinen 
interface BBus<V> {
  plug(x: Bacon.Observable<V>): void;
  plug2<OV extends V>(x: Bacon.Observable<OV>): void;
}

const b = (testBus as any) as BBus<TestObject>;

// this compiles without errors
b.plug2(Bacon.interval(1000, 0).map(() => ({ a: 999 })));

How about this: 02c28fd

Included in 3.0.4