Repository: TypeScript
Issue: Conditional type triggers "No error for last overload signature" exception
Labels: Bug,Fix Available,Rescheduled

# Bug Report

<!--
  Please fill in each section completely. Thank you!
-->

### 🔎 Search Terms

**"No error for last overload signature"** yields [stale issue 35186](https://github.com/microsoft/TypeScript/issues/35186), and a couple of more recent issues that got resolved: [48636](https://github.com/microsoft/TypeScript/issues/48636), [37974](https://github.com/microsoft/TypeScript/issues/37974). (The stale 35186 appears to be a different issue as it also occurs in versions before v5.0.4.)
<!--
  What search terms did you use when trying to find an existing bug report?
  List them here so people in the future can find this one more easily.
-->

### 🕗 Version & Regression Information

<!-- When did you start seeing this bug occur?

"Bugs" that have existed in TS for a long time are very likely to be FAQs; refer to
  https://github.com/Microsoft/TypeScript/wiki/FAQ#common-bugs-that-arent-bugs

If possible, please try testing the nightly version of TS to see if it's already been fixed.
For npm: `typescript@next`
This is also the 'Nightly' version in the playground: http://www.typescriptlang.org/play/?ts=Nightly

Note: The TypeScript Playground can be used to try older versions of TypeScript.

Please keep and fill in the line that best applies:
-->
- This is a crash
- This changed between versions 4.9.5 and 5.0.4, and still occurs up to typescript@next.

### ⏯ Playground Link

<!--
  A link to a TypeScript Playground "Share" link which shows this behavior.
  This should have the same code as the code snippet below, and use whichever settings are relevant to your report.

  As a last resort, you can link to a repo, but these will be slower for us to investigate.
-->
[Bug-workbench link with relevant code](https://www.typescriptlang.org/dev/bug-workbench/?ts=5.2.0-beta#code/PTAEAEDMEsBsFMB2BDAtvAXKALgTwA7wDOAdNkQFDwAe+A9gE7Y4HygAKDdAJgK4DG2AHJo2AXlAByZJNAAfKQCNJFKrUbM8hUAGVei-Fz6CR6AGKNOPAcNEAedqBrYk3IhyM3T8AHygJjs6u7ryIANaIdADuiBSgoAD8oGHwuHSQoACiiNjQucQAQrhWxrboANrsALpxoFiI8ABu8AyqWmzZuflERSVeov6gAN61yFhDoMgAjOOgjciwvJhSyAC007IAvqCbtYqzijPDcwtLWJKKq4dbOxS7avRMLNoAItBE-AzQqNAoLtwAVUQ0DoiDstUC1BciDcHmsJgGAU8CPQABpapknFDgrp9IZ4WV4BYGH0UfAHH4JHoDMjCcTSYSKejKRwsdDYQzvLV4klMUEYe5qfjSt56bTvBTuaAQIksmycaEItFENKwAB1PIAC1AisiMVROE1bBo-Hg+FyoNA3DoxEQkmYmuQ+EIiBIoClPKyOTy0EKxXFokqVXKmRq8XDdVADWarQj9SaLVUMqgcCQoiwTvwZEo30ezAmQoD5ksRfgBreHy+Pz+8EBwNBBs5A22kC4qCkJGA7SIKgeGlA-FBRGYil4AHNBg55QK4SLRD4ABQASn8fhG8UHiGHoCIeNpRCwFc+31+yH+QJBYPYBsLBNFJbv9nYPh85SqgzftV3NIJpFQToXBcAH1D3eY9qzPWsL1BFcxD8RBeFgWAlzuCggA)

### 💻 Code

```ts
// @filename: types.ts
export type ProductName = 'a' | 'b'

export type SubproductNameForProductName<P extends ProductName> = P extends unknown
  ? keyof EntitiesByProductName[P]
  : never

type EntitiesByProductName = {
  a: { a1: { value: 'a-a1' } }
  b: { b1: { value: 'b-b1' } }
}

export type DiscriminatedUnion<
  P extends ProductName = ProductName,
  E extends SubproductNameForProductName<P> = SubproductNameForProductName<P>,
> = P extends ProductName
    ? E extends SubproductNameForProductName<P>
    // ? E extends unknown // With unknown, the exception doesn't happen. 
      ? EntitiesByProductName[P][E]
      : never
    : never

// @filename: app.ts
import { SubproductNameForProductName, DiscriminatedUnion, ProductName } from './types'

export const bug = <P extends ProductName>() => {
  const subproducts: DiscriminatedUnion<P, SubproductNameForProductName<P>>[] = []
  subproducts.map((_: DiscriminatedUnion) => null)
}
```

### 🙁 Actual behavior

The compiler throws an exception:
```
/Users/martijn/git/Insify/insify-core/node_modules/typescript/lib/tsc.js:114747
      throw e;
      ^

Error: Debug Failure. No error for last overload signature
    at resolveCall (/Users/martijn/git/Insify/insify-core/node_modules/typescript/lib/tsc.js:70223:19)
    at resolveCallExpression (/Users/martijn/git/Insify/insify-core/node_modules/typescript/lib/tsc.js:70605:12)
    at resolveSignature (/Users/martijn/git/Insify/insify-core/node_modules/typescript/lib/tsc.js:70988:16)
    at getResolvedSignature (/Users/martijn/git/Insify/insify-core/node_modules/typescript/lib/tsc.js:71008:18)
    at checkCallExpression (/Users/martijn/git/Insify/insify-core/node_modules/typescript/lib/tsc.js:71120:23)
    at checkExpressionWorker (/Users/martijn/git/Insify/insify-core/node_modules/typescript/lib/tsc.js:74278:16)
    at checkExpression (/Users/martijn/git/Insify/insify-core/node_modules/typescript/lib/tsc.js:74189:32)
    at maybeCheckExpression (/Users/martijn/git/Insify/insify-core/node_modules/typescript/lib/tsc.js:73280:28)
    at BinaryExpressionStateMachine.onLeft (/Users/martijn/git/Insify/insify-core/node_modules/typescript/lib/tsc.js:73216:16)
    at Array.left (/Users/martijn/git/Insify/insify-core/node_modules/typescript/lib/tsc.js:25140:30)
```

### 🙂 Expected behavior

No compiler exception.