Repository: TypeScript
Issue: Nested conditional type inconsistently causes error with annotations
Labels: Bug

<!--
Please help us by doing the following steps before logging an issue:
  * Search: https://github.com/Microsoft/TypeScript/search?type=Issues
  * Read the FAQ: https://github.com/Microsoft/TypeScript/wiki/FAQ

Please fill in the *entire* template below.
-->

<!--
Please try to reproduce the issue with the latest published version. It may have already been fixed.
For npm: `typescript@next`
This is also the 'Nightly' version in the playground: http://www.typescriptlang.org/play/?ts=Nightly
-->
**TypeScript Version:**  4.0.3 & 4.1.0@beta

<!-- Search terms you tried before logging this (so others can find this issue more easily) -->
**Search Terms:** Type annotation wrongly overrides generic, nested conditional types infer union with annotated, nested conditional errors


**Code**

```ts
const createMatrix = <D extends number, T>(
    dimensions: D,
    initialValues: T | null = null
): Matrix<D, T> => {
    const currentDimensionLength = dimensions;
    const remainingDimensions = dimensions - 1;
    const needsRecursion = remainingDimensions > 0;

    const currentMatrix = Array(currentDimensionLength).fill(initialValues);

    const finalMatrix = needsRecursion
        ? currentMatrix.map(() => createMatrix(remainingDimensions, initialValues))
        : currentMatrix;

    return finalMatrix as Matrix<D, T>;
};
```
```ts
type Matrix<D extends number, T> = D extends 1
    ? T[]
    : D extends 2
    ? T[][]
    : D extends 3
    ? T[][][]
    : any[][][][];
```

**Expected behavior:**
Adding type annotations to variables should be compatible with the functions return signature, causing no type errors

**Actual behavior:**
Annotating variables before assigning them to the returned value of the function causes an error. The generic `T` is wrongly inferred to be _any of_ `array where depth < D` where it should be inferred to only the base type of the nested array. Removing the annotation solves the problem but removes type safety when variable assignment happens after creation.

```ts
const n1: number[] = createMatrix(1); // <- ✅ works - generic T is set to `number`
const n2: number[][] = createMatrix(2); // <- ❌ doesn't work - generic T is set to `number | number[]`
const n3: number[][][] = createMatrix(3); // <- ❌ doesn't work - generic T is set to `number | number[] | number[][]`

const n4 = createMatrix(2); // <- ✅ regular inference works - generic T is `unknown`, return type is unknown[][]
const n5: number[][] = createMatrix(2, 0); // <- ✅ works when initialValues is set - generic T is `number`, return type is number[][]
const n6: unknown[][] = createMatrix(2); // <- ✅ works fine, as type T is unknown. A more specific annotation should constrain `unknown` to `number`
```


The problem seems to be that (when annotated) the type is inferred to be the union of each type in `Matrix` that's preceding the conditional that returns true:

For a `Matrix` where `D === 2`, `number[][]` matches the `D extends 2 ? T[][]` branch and gets `T = number`, _but_ the preceding branches are also (wrongly?) matched, so in this case `D extends 1 ? T[]` matches, and `T = number[]` is also returned. The result is then the union `T = number | number[]`.

This happens inconsistently, as I've tried this pattern with various different returns from the same nested conditional. Objects with multiple keys work fine and can be annotated as normal without error. Objects with only one key (containing `T`) give the same error as with the `Matrix` type when annotated. All of these tests are in the Playground link (below).

**Playground Link:** [Link](https://www.staging-typescript.org/play?ts=4.0.5#code/PTAEDkFMGcBdICagMYHsB2CCWssYIYA2osAngA6ShbprrRZyTqyGkr4Cu0MokATv1T9QAdxwALUPnTpUsfLgzQAUCpCgAogA9KyeEgBGkCfgBuefgC51YAIIJs6AOYkKVGXIVL6JVKDN8fix8Q0JeaAlUTkIjKjQAW3JFLDCqcVgpTKoAM05aH2hQfkhYTn50UAZndEVyyAAaDm4aVzk3Sj5BYVVbUDt9TiJQY1MLYRsNO2hq9FbpSs95RUQAoJC0vxIJKhKyirXCTipUHO3c-P08SuQuHiKZLqF+ADo+gBUd0GdmASxkUDvahFURCFxsajoHICEpIWD+YwLdinUAAciC-Hw7FEOxKoAQkHImVAAB5QAARVFiXFUHBVKIxBB9RE0aGCVbw0AYCHZEb4HgdE5nXnoGAGaSCLEvUAAJUgCVQFhc5wWXhSGCqqEIZhgfV55CEaQSI04sGK8sVvDInWg+GhZGpzDWwVC4WkMywNQSzDNpnIlF8dvgImQJXV6DeamAACpoypQNHZaVyoHQAlFMFtKAABQJGK4AC02G99GuwwxWIAlGJJKBSNERASGLD8VgSwxlAskIEjjA3gnQAABZKY43F5gd3wFwFfJtYFvj0udlG89OwTPxxPDoL4Y00HAhQgANSIxyK08+VB7x22ij4+GQUnIqBoZpoKrXmchB6IjCt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