Repository: TypeScript
Issue: Generic types should be compatible
Labels: Bug,Help Wanted

# Bug Report

### 🔎 Search Terms

- generic types not equal

### 🕗 Version & Regression Information

- This is the behavior in every version I tried, and I reviewed the FAQ for entries about generics.

### ⏯ Playground Link

[Playground link with relevant code](https://www.typescriptlang.org/play?ts=5.1.3#code/FAehAIBUAsFMGdbgIYCckBc7gwTwA4LgD2AZuOgMbGoAm84AlgHY7a3IbIBGyiAdMDyEoPADaxIxAEqxqdcAF5wAb2ABIZAC5VKHcwCuAW26xU4AL4buOleBvh4GVCwDmljZVvgvj524BtAF0PKyECJEhxJGUAa1hcMlFuCSlZeVpgUAgAYXROJGRwA2ZGYlYklS4U2AAaJloLEm4AKzkMeEFhJAAFYhYMMwAeSHBYAA9B5npkiSVZ2AA+ebVwNfAAgGkmVkgg22qJHU36xlodJxdmVysLAL2ssHAACWIANzNwAA0cYkdYTDQRgMYEocD4VDEQjmEplVjdcIiH7KPoDMyPCCAUHI2KCAO40WIMPhjcaESiDWiCWhyMRoJCkErkuHgVywDDpGi0EYkqYzKI1RYACnw-WYg1QOlRYuGkEWAEodPzUjI5Jz7iFgNRmE5wON5qz2aq6IKqtEdAAiZDm07ncDm80WOUY8CAGXIcQx4AZUBDgW4xLhwLRiAhmAByDDgfGoWLAamUWnocAM5hM8qOYhGNlA67C0XiyV5swK8BvfqZLU6-AFtGoIaW83LZSmmoWq02i0O4DwDNZtzCp3J1OsVAlbkTXkMJVLXM16vS2uyuWqYDrHzlHV65TdzNYPsimtOsJPbGvXGwD6oeq7hhBohR2JMcgj1ig1nMMyMSgkVhS8U7JywMgtAkOQU5UjSdJJoyGDMtuvbXAATDO85zuKxalmcwCDjBabPghQw9DysDTAwv5mEK+4oeAPRLqs6wVhGm7pju2auMhaHAEeWLgAA6kClDQGwxHgEYyDxAwmaRuU1LmIw5CMB09hsn+GB-LiLiDCybIcgo16RsguCxhBibYcyBo6bQ+GEeOxEzGRqAUYWErUcWU5pEatABD0ATmocsDmkEGoMbqCH6tpHlIc2Rx2m2DQdo6wBAA)

### 💻 Code

```ts
// These are the types of records in the database.
type TableToRecord = {
	a: { a: number }
	b: { b: string }
	c: { c: string[] }
}

type Table = keyof TableToRecord

// Create a union of {table, id} objects.
type Pointer<T extends Table = Table> = {
    [K in T]: {table: K, id: string}
}[T]

// Hover X to see this is a proper union type
type X = Pointer

// ✅ this works as expected.
declare function getRecord<T extends Table>(pointer: Pointer<T>): TableToRecord[T] 
const x = getRecord({table: "a", id: ""})

// ❌ this surprisingly doesn't work
declare function something(pointer: Pointer): void
const p: Pointer<"a"> = {table: "a", id: ""}
something(p)
function run<T extends Table>(pointer: Pointer<T>) {
    const x = something(pointer)
}

// ✅ However, this does work if run is generic on Pointer instead of Table.
declare function something2(pointer: Pointer): void
function run2<P extends Pointer>(pointer: P) {
    const x = something(pointer)
}

// ✅ Which then makes me wonder if its better to write getRecord this way
declare function getRecord2<P extends Pointer>(pointer: P): TableToRecord[P["table"]] 
const x2 = getRecord2({table: "a", id: ""})
```

### 🙁 Actual behavior

I'm surprised by this error where `Pointer<T>` where `T extends Table` doesn't satisfy the argument `Pointer<Table>`.

If we were talking arrays, `Array<T>` where `T extends string | number`, I'd imagine you should be able to pass that to a function that accepts `Array<string | number>` as an argument. But it is a bit tricky if that argument gets mutated by the function, e.g. pushing a number onto a string array. I think there's a fancy type-system word for this behavior?

But as I understand it, TypeScript is all structural comparison and since `Pointer<T>` unfurls into the union type, I'm curious where the problem lies and it seems to me like the type system should let this work...

I noticed when the generic param is `P extends Pointer` instead of `T extends Table` and then using `Pointer<T>`, then the code does work. But then that leads me to wonder if there's any difference between `function getRecord<T extends Table>(pointer: Pointer<T>): TableToRecord[T]` and `function getRecord2<P extends Pointer>(pointer: P): TableToRecord[P["table"]] `...

<!-- What happened, and why it was wrong -->

### 🙂 Expected behavior

<!-- What you expected to happen instead, and why -->