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 })));
This is a likely simple fix per se, but to have the whole project compile with `—strict` will take a lot of effort. I’d like to have a failing test first and then improve typings.
… On 15 Jun 2019, at 15:21 , Antti Risteli ***@***.***> wrote:
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 })));
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub <#738?email_source=notifications&email_token=AACHQZERL7MIBM47CN2QBCLP2TNFDA5CNFSM4HYO5V5KYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4GZWEKFQ>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AACHQZCCXZH4TQISFSU4HETP2TNFDANCNFSM4HYO5V5A>.
How about this: 02c28fd
Included in 3.0.4