getting inferences working with typescript pubsubber So I'm learning typescript and I'm making a simple functional PubSub system. I want the user to be able to define topics and the data for those topics. So far I've got the below code, but I really want to improve it.
I'm not sure if I can get rid of the unknown in &lt;T extends Record&lt;string,unknown&gt;&gt;. I don't think that's a huge issue for me. I'd really like to get rid of the Set&lt;Subscriber&gt; part of the map that holds the subscribers. I tried something simple like Set&lt;Subscriber&lt;T[keyof T]&gt;&gt; but then it complains below where I am using &lt;R extends keyof T&gt;. Maybe that's the part that needs fixing?
Any and all suggestions welcome of course!
type Subscriber&lt;R&gt; = (data: R) =&gt; void;

const pubSubber = &lt;T extends Record&lt;string, unknown&gt;&gt;() =&gt; {
  const subscribers = new Map&lt;keyof T, Set&lt;Subscriber&lt;any&gt;&gt;&gt;();
  return {
    subscribe: &lt;R extends keyof T&gt;(topic: R, subscriber: Subscriber&lt;T[R]&gt;) =&gt; {
      const current = (subscribers.get(topic) || new Set()) as Set&lt;
        Subscriber&lt;T[R]&gt;
      &gt;;
      current.add(subscriber);
      subscribers.set(topic, current);
      return () =&gt; current.delete(subscriber);
    },
    publish: &lt;R extends keyof T&gt;(topic: R, data: T[R]) =&gt; {
      const toCall = Array.from((subscribers.get(topic) || [])) as Subscriber&lt;T[R]&gt;[];
      toCall.map((f) =&gt; f(data));
    },
  };
};

type Topics = {
  gear_with_radius: {
    center: number;
    radius: number;
  };
  outside: {
    help: boolean;
  };
};
const myPubSubber = pubSubber&lt;Topics&gt;();
myPubSubber.publish('gear_with_radius', { center: 10, radius: 5 });
I have a few pointers regarding your snippet.
Return type
Let's start with pubSubber function return type. It's very nice that typescript infers types, but it also a good way to make a mistake. The bad thing about it is that you can accidentally change the return type without knowing it. If you use this function somewhere in the app there is a chance that typescript will notify you about the change. But if you are creating a library and don't have tests, you will change the return type without any error.
Personally, I like the rule to always specify return type (unless using inline arrow functions). I even use the lint rule to enforce this.
Don't use any
Change any to unknown. In this case, maybe it doesn't matter at least at the moment, but in the future, you may change this code. With any you can do anything and typescript won't say anything. With unknown you can't do anything, unless you cast it or use type Type Guard
const subscribers = new Map&lt;keyof T, Set&lt;Subscriber&lt;any&gt;&gt;&gt;()

Don't use cast if possible
Let's look into this line.
const current = (subscribers.get(topic) || new Set()) as Set&lt;
  Subscriber&lt;T[R]&gt;
&gt;;

current real type is Set&lt;Subscriber&lt;any&gt;&gt;, but you cast it to Subscriber&lt;T[R]&gt;. But in this example, there is no point having cast at all, because even without it the code is correctly typed. This function implementation works with any type, so you can leave it as it is. As for the outer interface, it can be more strict.
const current = subscribers.get(topic) || new Set()
current.add(subscriber);
subscribers.set(topic, current);
return () =&gt; current.delete(subscriber);

Second example is a bit better:
const toCall = Array.from((subscribers.get(topic) || [])) as Subscriber&lt;T[R]&gt;[];
toCall.map((f) =&gt; f(data))

You already had one mistake because of this cast. Also, I don't see any reason to convert Set to the list. You can just loop the Set. And if you combine it with optional chaining, it will look like:
subscribers.get(topic)?.forEach(f =&gt; f(data));

Better typing
In the previous section, I mentioned casts. But removing casts in theory you lose some typings (adding also lose some typings, dilemma...). Let's try to have full correct typings without any cast. So to start with we need to change Map to Object. Map types do not allow relations between key and value (e. g.: a key must-have number value`).
Example of relation-based subscribers list:
const subscribers: {
  [P in keyof T]?: Set&lt;Subscriber&lt;T[P]&gt;&gt;
} = {}

Final solution
So the final solution can looks like:
type Subscriber&lt;R&gt; = (data: R) =&gt; void;
type BaseTopics = Record&lt;string, unknown&gt;

interface PubSubber&lt;T extends BaseTopics&gt; {
  subscribe: &lt;R extends keyof T&gt;(topic: R, subscriber: Subscriber&lt;T[R]&gt;) =&gt; () =&gt; void
  publish: &lt;R extends keyof T&gt;(topic: R, data: T[R]) =&gt; void
}

function pubSubber&lt;T extends BaseTopics&gt;(): PubSubber&lt;T&gt; {
  const subscribers: {
    [P in keyof T]?: Set&lt;Subscriber&lt;T[P]&gt;&gt;
  } = {}
  return {
    subscribe(topic, subscriber) {
      const current = subscribers[topic] || new Set();
      current.add(subscriber);
      subscribers[topic] = current;
      return () =&gt; current.delete(subscriber);
    },
    publish(topic, data) {
      subscribers[topic]?.forEach(f =&gt; f(data));
    },
  };
};