Skip to content

Commit

Permalink
src: Improve types for natives like String.
Browse files Browse the repository at this point in the history
  • Loading branch information
zorbyte committed May 22, 2020
1 parent ba41aa2 commit d978a74
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 7 additions & 2 deletions example/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ class Test {

const m = new PubSub();

m.subscribe(Test, (b) => {
console.log(b);
m.subscribe(Test, (t) => {
console.log(t);
});

m.subscribe(String, (s) => {
console.log(s);
});

m.publish("sdfg");
m.publish(new Test());
5 changes: 4 additions & 1 deletion pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ export type Constructable<T> = { new (): T };
export class PubSub {
private subscribers = new Map<unknown, Subscriber<any>[]>();

public subscribe<T>(subscriptable: Constructable<T>, cb: Subscriber<T>) {
public subscribe<T extends Constructable<unknown>>(
subscriptable: T,
cb: Subscriber<InstanceType<T>>,
) {
const subs = this.subscribers.get(subscriptable) ?? [];
subs.push(cb);
this.subscribers.set(subscriptable, subs);
Expand Down

0 comments on commit d978a74

Please sign in to comment.