Repository: TypeScript
Issue: Type unification not working for nested types
Labels: Bug,Domain: Variance Relationships

**TypeScript Version:**  3.7.2, 3.6.3, 3.5.1 (but not 3.3.3)

**Search Terms:**
type unification

**Explanation for the code:**
We have a collection of different document types (think Firebase's Cloud Firestore if you're familiar with that). Each document type of which has a type associated with it which contains a few fields like the document type, the backend return type, etc. Then we have a number of functions in our store that take in one of these types and use the specific fields that they need. (Some functions will look at the `foo` field, some will not.) It's not at all uncommon for these to be passed through lots of wrapping functions until the last level pulls out the field that it needs.

It is nice sometimes to be able to share code between two different (but similar) collection types. We can do if say their document type is the same (when two different endpoints return the same document). With recent versions of TypeScript we can only do that sharing if we expand the types at the place of definition.

**Code**
```ts
// A type that just pulls the foo field out of the type
type JustFoo<T extends { foo: any }> = T['foo'];

// This type is unused, but we have types like this too.
type JustOtherField<T extends { otherField: any }> = T['otherField'];

// We then wrap this in another type
type WrappedJustFoo<T extends { foo: any }> = { wrapped: JustFoo<T> };

// We then have a lot of types that have a `foo` field and `otherField` fields.
// These types will be passed into a lot of other functions, some of which
// will only use the `foo` field, where as other function may use other parts.

type Alpha = { foo: number; otherField: string };
type Beta = { foo: number; otherField: boolean };


// We can define variables then using `WrappedJustFoo`.
const alpha: WrappedJustFoo<Alpha> = { wrapped: 3 };
const beta: WrappedJustFoo<Beta> = { wrapped: 3 };

// Sadly we can't share them even though ` WrappedJustFoo<Alpha>` and
// `WrappedJustFoo<Beta>` actually resolve to the same type.
const beta2: WrappedJustFoo<Beta> = alpha;

// If we use the resolved type we can convert either way.
let gamma: { wrapped: number } = alpha;
let beta3: WrappedJustFoo<Beta> = gamma;
```

**Expected behavior:**
The program compiles.

**Actual behavior:**
```
Type 'WrappedJustFoo<Alpha>' is not assignable to type 'WrappedJustFoo<Beta>'.
  Type 'Alpha' is not assignable to type 'Beta'.
    Types of property 'otherField' are incompatible.
      Type 'string' is not assignable to type 'boolean'.(2322)
```

It makes sense that if you want to check that `'WrappedJustFoo<Alpha>'` is assignable to type `'WrappedJustFoo<Beta>'` you only check if `'Alpha'` is assignable to type `'Beta'`, but this isn't the only way they can be compatible.

<!-- Did you find other bugs that looked similar? -->
**Related Issues:** Nothing jumps out at me. #30134 maybe?



<details><summary><b>Compiler Options</b></summary>

```json
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}
```


</details>

**Playground Link:** [Provided](https://www.typescriptlang.org/play/index.html#code/PTAEEFQFwTwBwKbQBYEMqgFYFcDOG5sAbI3FJAMwHsrQKBLBIgE1CuwyovOngQChYiUACk8UAGI0APABVQCAB5QEAO2ZkA3nRoAuUKlUxQAXwB8oALyhZAbQDk1KvYC6Abn78QN5PTJCkP1BsVTwEZgAaUAAjDlAAdyQ0ADckALIiegBrNN9-GgA6QT5RcQB5KGQEACcJRhY5BWU1DVBtKkqauqZmfUNjcysbBw6q2vrmVw8vMAB1XLUE6tQ4FCD6VQNVUZreRGLhWeW4RGYxfCkqRqUVdS0dKj6jUwtrbXjj0-1zyRlZCxM02883ImxSSFQoCIHTY3HSKHQoHBBlAAAMnKi6BMtqxUTtxj1MQwergit5ZFVcGk+GR4vQSDEkHBULgqawNlBaJDoZxuPi6CEAMZQehUVS4KK4KgAWyQXASvkFyBmCXpRDYqiIxjCPHRNCJEyi8TGELI-IoQpFYtA0tQ2qpbE61VAzOqUFJngCECIcDQQ20Tn0oWl0RqbkdY26LH0+GqGwA5qYPF6AEIIKCQt4PIPYENhiNdCb6aI0IgIQxJzwqkGCivMBAMVRIZKoOOoaJl-xVTZ4BNoo4rU4-S6ooqCsX4Aw+tD6AcncLDmTgaeoV5tJaD8L6ADMlfH4owoYzs8+C-El2kaYza-ep96oF3gM83gAyqhmFqEkha6p7BhcGg1QLNKCipJslTsPGyBoqAc5DueS4rmYmKGMwKqonBZ4XDIV6rihwrYKgJDGEBUpEKk0C0J0oC4Kgsp7AgY4Toe6aoAATCem5nAhVy4WuRG+qgQJgAAktwiTBA61GkVQ5HhAxX6gD+SliqkboKPQToJHaRRlhg8Z0ba+i3lxOZ5s6JhDAJaAeHpjIZtunHztx2G8axa4GdKtoeEAA)