Repository: TypeScript
Issue: TypeScript should have optimized this conditional check
Labels: Help Wanted,Possible Improvement

### 🔎 Search Terms

overloads, conditional types, aliases, performance, caching

### 🕗 Version & Regression Information

- This is the behavior in every version I tried, and I reviewed the FAQ for entries about aliases and conditional types.

### ⏯ Playground Link

https://github.com/solana-labs/solana-web3.js/pull/1760

### 💻 Code

# Context

I had a large conditional type of the form:

```ts
type Overloads<T> =
    T extends {
        (...args: infer A1): infer R1;
        (...args: infer A2): infer R2;
        (...args: infer A3): infer R3;
        (...args: infer A4): infer R4;
    }
  ? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3, (...args: A4) => R4]
  : T extends {
        (...args: infer A1): infer R1;
        (...args: infer A2): infer R2;
        (...args: infer A3): infer R3;
    }
  ? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3]
  : T extends {
        (...args: infer A1): infer R1;
        (...args: infer A2): infer R2;
    }
  ? [(...args: A1) => R1, (...args: A2) => R2]
  : T extends {
        (...args: infer A1): infer R1;
    }
  ? [(...args: A1) => R1]
  : unknown;
```

[This refactor](https://github.com/solana-labs/solana-web3.js/pull/1760) reduced check times in my project by 100x:

```ts
type Overloads<T> = Overloads4<T>;
type Overloads4<T> = T extends {
    (...args: infer A1): infer R1;
    (...args: infer A2): infer R2;
    (...args: infer A3): infer R3;
    (...args: infer A4): infer R4;
}
    ? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3, (...args: A4) => R4]
    : Overloads3<T>;
type Overloads3<T> = T extends {
    (...args: infer A1): infer R1;
    (...args: infer A2): infer R2;
    (...args: infer A3): infer R3;
}
    ? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3]
    : Overloads2<T>;
type Overloads2<T> = T extends {
    (...args: infer A1): infer R1;
    (...args: infer A2): infer R2;
}
    ? [(...args: A1) => R1, (...args: A2) => R2]
    : Overloads1<T>;
type Overloads1<T> = T extends {
    (...args: infer A1): infer R1;
}
    ? [(...args: A1) => R1]
    : unknown;
```

# Repro

```
# Check out the repo before the change
git clone git@github.com:solana-labs/solana-web3.js.git
cd solana-web3.js
git checkout ce145c805ee15ce0c5ef97623d9cfd006cf0b00b
pnpm i
cd packages/library/
pnpm turbo compile:js compile:typedefs
pnpm tsc -p ./tsconfig.json --noEmit --generateTrace tracing_output_folder
# Open tracing_output_folder/trace.json in chrome://tracing

rm -rf tracing_output_folder/

# Move to the commit after the change
git checkout ba9b4c72cb0dfc0a36fed1a0641a19492dd94624
pnpm turbo compile:js compile:typedefs
pnpm tsc -p ./tsconfig.json --noEmit --generateTrace tracing_output_folder
# Open tracing_output_folder/trace.json in chrome://tracing
```

### 🙁 Actual behavior

Check times in the project were >10s and Intellisense recomputed the entire project on every keystroke (ie. it took >10s to get typehints and autocomplete).

### 🙂 Expected behavior

TypeScript should have been able to optimize (ie. cache) this check on its own, without the need for the developer to manually alias each conditional type.

### Additional information about the issue

_No response_