Stack Exchange
Extracting type of object properties using generics
I have this following code
type GTMCreator<Payload> = (payload: Payload) => "a";

interface BudgetPayload {
  a: 2;
}
interface MarketPayload {
  b: 2;
}

interface Events {
  budget: GTMCreator<BudgetPayload>;
  market: GTMCreator<MarketPayload>;
}

const budget: GTMCreator<BudgetPayload> = (payload) => "a";
const market: GTMCreator<MarketPayload> = () => "a";

const events: Events = {
  budget,
  market
};

type Push = <T extends keyof typeof events>(
  event: T,
  ...payload: Parameters<typeof events[T]>
) => void;

const something = () => ({ push: (items: any) => "s" });

const push: Push = (event, payload) => {
  something().push(events[event](payload));
};

I expected the typeof payload to be inferred based on the event name passed as the first parameter. It is now inferring as

Type 'BudgetPayload | MarketPayload' is not assignable to type
'BudgetPayload & MarketPayload'.

Why is it not inferred as 'BudgetPayload | MarketPayload' ?
Code sandbox: https://codesandbox.io/s/thirsty-poitras-0d8qh?file=/src/index.ts

See if this helps

event and payload as unspecified generic types end up looking like two uncorrelated union-typed values to the compiler.  You know they are correlated and that event and payload should match up, but the compiler does not realize this.   See the answer to the question this duplicates for possible ways to proceed.

Like this code
