Repository: TypeScript
Issue: Fails to expand indexed access type when used as a generic type parameter
Labels: Help Wanted,Possible Improvement

# Bug Report

This logic is part of a proprietary DSL where the user wants to work with a number of types, specified as a record. Most of the time the user calls a function similar to
 ```ts
declare function someOtherFunction<T extends DiscriminatorRecord>(record: T): { [K in keyof T]: DiscriminatedUnion<T[K]> }
```
in which case there are no problems with the inference, but sometimes `T` needs to be restricted to have certain keys and values, as shown in the code below. One part of the problem that I wasn't able to reproduce in a code example was the fact that it did work with four types in the union, but failed when I added a fifth. Whereas in the example it fails with any number except one. I could spend more time on reproduction, but hopefully this will be helpful to start with.

### 🔎 Search Terms

generic indexed access type inference

### 🕗 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 the behavior in every version (`5.0.4`, `5.1.3`, `v5.2.0-dev.20230625`) I tried, and I reviewed the FAQ for entries about [Type System Behavior](https://github.com/Microsoft/TypeScript/wiki/FAQ#type-system-behavior), [Generics](https://github.com/Microsoft/TypeScript/wiki/FAQ#generics)

### ⏯ 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.
-->
[Playground link with relevant code](https://www.typescriptlang.org/play?ts=5.2.0-dev.20230625#code/C4TwDgpgBAIglgZwMYCc4Fs4DsCGwD2KUAvAFBRQA+UA5AGJwoLA3lW0DKES+WAJqwrUaAFQAWjAaVKhIsRKgzY8EPgFUscXgB4YUCAA9gEfgnnI0mXARQA+ElADeUPgsvKbALlhQAvlAAyKAAKNmpnVwsla0JvekZmGgAaKAAzBOBvACN8fAAbCBwsPzCnFzdovFjObl4BFIRa-mzcgqKSoTLIxSsqlDjxSWSoYAkUPhb8wuLfNgBKaVloeCjemwAlWvGHTZ5x7WY0LABzFJWej0JbaVSAVywkYC1ihHx0CDp7x+ftEX0jEx8MzndwxFC7Qh8QJlfAANwgKDQfAgAzGAj8tmCcycbCW5guYLoKDef2IUBEAG0aHCEUiIDQALpQAD0zKgAElUiMJGZDGAikCRvhaINxqxceBlhU1qoiW8ADJwYwoHB5BwgyrGdSaHSiNE0eys7kmbmIaQUPEamV8OXoUn40EqbU-K2XFC2kSGtkIMT4W55KFZaAQACOtzgsNVJmAQtNCHNUB4WGYaQAjA5ghMHZrZcT0IrlarscR7HwAHSjSQAbhZbJAEBjlbMAHdCABreMUJMp1IAJgzWdd1lzJOLpYraJrRqytxjWHwjZ5pFmQA)

### 💻 Code

<!-- Please post the relevant code sample here as well. This code and the Playground code should be the same, do not use separate examples -->
```ts
type Discriminator =
  | 'First'
  | 'Second'
  | 'Third'

type DiscriminatedUnion<D extends Discriminator> = { discriminator: D } & (
  | { discriminator: 'First', first: boolean }
  | { discriminator: 'Second', second: boolean }
  | { discriminator: 'Third', third: boolean }
  )

type DiscriminatorRecord = Record<string, Discriminator>

function someFunction<T extends DiscriminatorRecord & { override: 'Third' }>() {
  type DiscriminatorFromT = T['override'] // If this expands to 'Third'

  type DiscriminatedFromLiteral = DiscriminatedUnion<'Third'> // then this

  type DiscriminatedFromT = DiscriminatedUnion<DiscriminatorFromT> // should be equivalent to this

  const f1 = (d: DiscriminatedFromLiteral) => d.third; // yet this works
  const f2 = (d: DiscriminatedFromT) => d.third; // but not this
}

```

### 🙁 Actual behavior

`const f2 = (d: DiscriminatedFromT) => d.third;` fails with 

```
Property 'third' does not exist on type 'DiscriminatedUnion<DiscriminatorFromT>'.
  Property 'third' does not exist on type '{ discriminator: DiscriminatorFromT; } & { discriminator: "First"; first: boolean; }'.
```

### 🙂 Expected behavior

The property `third` is inferred to exist on `d` in `const f2 = (d: DiscriminatedFromT) => d.third;`