Repository: TypeScript
Issue: Inferences made when trying to match an overload are carried over to matching consecutive overloads
Labels: Bug,Needs Investigation,Rescheduled

# Bug Report

### 🔎 Search Terms

overload inference generic consecutive subsequent following 

the only issue that I have found that **might** be related to this is [this one](https://github.com/microsoft/TypeScript/issues/39850) but I can't assess on my own if they are the same or not

### 🕗 Version & Regression Information

This is the behavior present in all 4.x versions available on the playground, including 4.5-beta.

### ⏯ Playground Link

[Playground link with relevant code](https://www.typescriptlang.org/play?ts=4.5.0-beta&ssl=52&ssc=23&pln=46&pc=23#code/JYOwLgpgTgZghgYwgAgCoE8AOEDmEQAiwAznAEYA2EAJsgN4BQyyARAAJsAexYckA9GCy58LAFzJ4FYhADcDAL4NQkWIhQZseEAFEQ5KrUbN2XHnwiDh28cjBQArnMXLw0eEmQ6AbvjAB5MgArCAQweiY7YQkeKFAceSUVd3VkAEEw4AB7EECQsIAeVB8-ZAhOSBBqYi9fcDzQsAA+COYAfWKJYrqwRIZXVQ8UAGVeSABZRAALUAgCyO7S8srq2r8GsIAaBc0IYnGIXmQAXjRrfCJSShoGFuNkDp0ukvB5dtQAJVRxrt39w7gfQGKU8kwQMxAEH8mDA2RAxCKL3Cy3wqyRG2arWQiFhOQA-BIMrjcsFGoiek0gUJsMhJugyBBdtowRCoTC4QiFkiyhVUTV0aStjthP8jqcmRcSAYbi1xX8DkcUVUahLdPprtRIniscxmDi4QT0pkchiCnQotgJCwAOoASVQAAkOgBNAAKOgA4joAHIsZAKSmRZhKZgSFmzaHEhGLcCBhjUUIUOBQFAwBwgY0gbHEYjAHAgclLXnKtb1QXNAAUkTgObzIAAtn4JBWIN5nj0AJQnFreLLATUdwmZ00x5ryBj8fjYsD15C25BTOC+bHIXMgHBUVd1vgOFPYqrIYDhABUx-rfHBe1PE6nUyyAHdW9BNoeYHPkOmEFl643wHYZjUcDUNQR5wnAFDIFkvhQBQWRAf++DIKeIBZFu+Y7imp7IOeYCXsQN6vsgSaqJBWbzimWRQAmUAIWh+hgLuewIVmYBTKmwBQDwpEoBWrEoDkKCsXwh41Agu4puAFDoMgKHhF+P5+DQkEOGAXY4Xh-STsg96LuEi6YNg8ILtAKAkP+wnEN+pkgDAJkZkx54JtpbFZnwkD1uy652Kh6lTLRMAcVxUHQLB8HJigCDTEpBFwAeLCRVAcRKcFUB+mAPkXhCOC0TIX4HiloW0AAtM5wDgthcAANZMXxzlZJurHxJIcDABQmlTgmCBJnuaYZsSH4yOGkLzFpzCjjyKz8j0GLbKNaDygCE18mcWj4Ho0qalpTRVnNeBgENEASKMFgHYW4AvqgC28E0s1TswWTsjkxBhnA9KMucIAHZGHJnWAF1XXATQEYO9CSIFYASPYTj+vInXdamn79Q4g3TLMv1LSWAr5JWkR7QdR1jBAp2jjdkQPVGhpfY98K-UDIPmrlOTUJDjgoAo455Vx57grMJz0Ao2bIMdEyo8N5rUodrB2o6-gAKqoC67per6-pxpz4QpuEpzI0TosQBW3Osi+9z6jkEg1rm+YVhWbStl2xx3EGyDW20EtWtLDpywrqBup6PosPbLQgA4FAUB2NutgAdBLHZvP6HbbAosftcgQTI+E0HADA0k6WVfkFXBtCLjUDKIeCWRZDIIAEWwYDEEV5TYGEDeJZRDCa5HAWcb07eHJHjNVPIQA)

### 💻 Code

```ts
interface TypegenDisabled {
  "@@xstate/typegen": false;
}
interface TypegenEnabled {
  "@@xstate/typegen": true;
}
interface EventObject {
  type: string;
}
interface ActionObject<TEvent extends EventObject> {
  _TE: TEvent;
}

interface StateMachine<
  TEvent extends EventObject,
  TTypesMeta = TypegenDisabled
> {
  _TE: TEvent;
  _TRTM: TTypesMeta;
}

interface MachineOptions<TEvent extends EventObject> {
  action?: ActionObject<TEvent>;
}

type MaybeTypegenMachineOptions<
  TEvent extends EventObject,
  TTypesMeta = TypegenDisabled
> = TTypesMeta extends TypegenEnabled
  ? {
      action?: ActionObject<{ type: "WITH_TYPEGEN" }>;
    }
  : MachineOptions<TEvent>;

declare function assign<TEvent extends EventObject>(
  assignment: (ev: TEvent) => void
): ActionObject<TEvent>;

// atm I have a single signature and it **matches**
// however, if I uncomment this additional overload then **no signature** matches
// if later on I reorder the signatures then the first one (the one that is currently not commented out) matches

// what happens here is that some inferences made when attempting to match the first overload are cached 
// and "carried over" to matching the second overload - which makes the whole thing fail

// declare function useMachine<
//   TEvent extends EventObject,
//   TTypesMeta extends TypegenEnabled
// >(
//   getMachine: StateMachine<TEvent, TTypesMeta>,
//   options: MaybeTypegenMachineOptions<TEvent, TTypesMeta>
// ): { first: true };
declare function useMachine<TEvent extends EventObject>(
  getMachine: StateMachine<TEvent>,
  options?: MachineOptions<TEvent>
): { second: true };

const machine = {} as StateMachine<{ type: "WITHOUT_TYPEGEN" }>;

const ret = useMachine(machine, {
  action: assign((_ev) => {
    ((_type: "WITHOUT_TYPEGEN") => null)(_ev.type);
  }),
});
```

### 🙁 Actual behavior

No overload signature matches 

### 🙂 Expected behavior

I would expect a signature to match when it's used as one of the overloads if it matches on its own. And I would expect for inferences made in a failed attempt to match a signature to have no effect on matching the following signatures.