Repository: TypeScript
Issue: Max depth limit does not trigger. Gives up and resolves type to any
Labels: Bug

<!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨

Half of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Even if you think you've found a *bug*, please read the FAQ first, especially the Common "Bugs" That Aren't Bugs section!

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 `typescript@next`. It may have already been fixed. -->
**TypeScript Version:**  3.5.1

<!-- Search terms you tried before logging this (so others can find this issue more easily) -->
**Search Terms:**

+ Type instantiation is excessively deep and possibly infinite.ts(2589)
+ Chaining methods
+ Fluent API
+ Generic class
+ Generic method

**Code**

Using `DataT`,
```ts
type AppendToArray<ArrT extends any[], T> = (
    (
        ArrT[number]|
        T
    )[]
);
interface Data {
    arr : number[]
};
type AppendToFoo<C extends Data, N extends number> = (
    Foo<{arr:AppendToArray<C["arr"], N>}>
);

class Foo<DataT extends Data> {
    arr! : DataT["arr"];
    //Using `DataT`
    x<N extends number> (n:N) : AppendToFoo<DataT, N> {
        return null!;
    }
}
declare const foo0 : Foo<{arr:0[]}>;
/*
const foo12: Foo<{
    arr: (0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12)[];
}>
*/
const foo12 = foo0
    .x(1).x(2).x(3).x(4).x(5).x(6).x(7).x(8).x(9).x(10).x(11).x(12);
/*
Expected:

+ Similar to foo12
OR
+ Type instantiation is excessively deep and possibly infinite.ts(2589)

Actual:
const foo13: Foo<{
    arr: any[];
}>
*/
const foo13 = foo12.x(13);
```

[Playground](http://www.typescriptlang.org/play/#code/C4TwDgpgBAgmkDsAmAVA9jATpghiAPFpilBAB7ATIDOUOCIA2gLoA0UKAfFALxQAUAKCgiBw0RKIpGCAK4BbAEYRMzAD7iJIlJqgBKFoL0BuQQEsElTADMcAY2gARHMBxQA3rpzYoALihySiqGAL6moJCw8FSoaABiaGj4AMKkFDG0zq7sAHJplDQBCsqY3HxCEglJ7t6YvnCIsUR4KYwARLVtbFA5nCGcRqaCdgA2ONS0VfhZOCTkBUiZLjjcnhK1AIR+UDPSHdhdphIA9McAqtQWAOZQAAa7t7pk+HnzGUVBpQIIvjl62w0YugprtcqtdBJMBBgLJMAgiiMRhsjqIQoI0UgIKNvNA7GgENRgFBrIkAAzbKY1bC+UksfqmY4AKmG+MJxMSAEYAEz+SleakCclqKAcqDCrliqAAZklABZJQBWSUANklAHZJQAOSUATklHKFItFwu5BmYpn6gkZxxZBKJJLQ3N47LQpN0ADoyPwOXpPfwub6vVLA-xZSGFSHlSG1SHNSGdSGDYmfX7TQzmQBRMiQOyUJC+QSCADUUAAymZ5GYxpgoMA0C7uYIAPIAJWLHHA0AshPowDMLjM+KgZlo5AcEzMADcICMQFBMRAwHRkFAwGgJ4pZ8OENYLGZKO7gNR-QrNQnCzBc7IcCMC3i7Q2pbzEvg1qJav56ExzeiBtbbWyDocjKfBAVyqbBsYQA)

Using `this`,
```ts
type AppendToArray<ArrT extends any[], T> = (
    (
        ArrT[number]|
        T
    )[]
);
interface Data {
    arr : number[]
};
type AppendToFoo<C extends Data, N extends number> = (
    Foo<{arr:AppendToArray<C["arr"], N>}>
);

class Foo<DataT extends Data> {
    arr! : DataT["arr"];
    //Using `this`
    x<N extends number> (n:N) : AppendToFoo<this, N> {
        return null!;
    }
}
declare const foo0 : Foo<{arr:0[]}>;
/*
const foo9: Foo<{
    arr: (0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9)[];
}>
*/
const foo9 = foo0
    .x(1).x(2).x(3).x(4).x(5).x(6).x(7).x(8).x(9);
/*
Expected:

+ Similar to foo9
OR
+ Type instantiation is excessively deep and possibly infinite.ts(2589)

Actual:
const foo10: Foo<{
    arr: any[];
}>
*/
const foo10 = foo9.x(10);
```

[Playground](http://www.typescriptlang.org/play/#code/C4TwDgpgBAgmkDsAmAVA9jATpghiAPFpilBAB7ATIDOUOCIA2gLoA0UKAfFALxQAUAKCgiBw0RKIpGCAK4BbAEYRMzAD7iJIlJqgBKFoL0BuQQEsElTADMcAY2gARHMBxQA3rpzYoALihySiqGAL6moJCw8FSoaABiaGj4AMKkFDG0zq7sAHJplDQBCsqY3HxCEglJ7t6YvnCIsUR4KYwARLVtbFA5nCGcRqaCdgA2ONS0VfhZOCTkBUiZLjjcnhK1AIR+UDPSHdhdphIA9McAqtQWAOZQAAbAABZm1Le6ZPh58xlFQaUCCL4cnptg0Yugpo9nrlVroJJgIMBZJgEEURiMNkdRCFBNikBBRt5oHY0AhqMAoNZEgAGbZTGrYXxUlj9UzHABUwxJZIpiQAnP46V4GQIaWooABGKBigBMUqgAGY5QAWOUAVjlADY5QB2OUADjlvIMzFM-UEbOOnNJ5MpaF5vB5aCpugAdGR+OK9G7+NKve75X7+ErA6rAxrA9rA3rA0bWRyAKJkSB2ShIXyCQQAaigAGUzPIzGNMFBgGhHbzBAB5ABKWY44GgFjJ9GAZhcZhJUGeaQcEzMADcICMQFA8RAwHRkFAwGg+4ph12ENYLGZKC7gNQfaq9UaMzAU7IcCN08TrY7xVSBYl8GtRLV-PQmCacQMLVbubaLw7bbzvReTEAA)

**Expected behavior:**

Both examples should resolve correctly or trigger, `Type instantiation is excessively deep and possibly infinite.ts(2589)`

See code for more details.

**Actual behavior:**

Both examples resolve to `any` without errors.
Both examples seem to have a different limit before this happens?

See code for more details.

**Playground Link:** 

Using `DataT`, the limit seems to be `12`.

[Playground](http://www.typescriptlang.org/play/#code/C4TwDgpgBAgmkDsAmAVA9jATpghiAPFpilBAB7ATIDOUOCIA2gLoA0UKAfFALxQAUAKCgiBw0RKIpGCAK4BbAEYRMzAD7iJIlJqgBKFoL0BuQQEsElTADMcAY2gARHMBxQA3rpzYoALihySiqGAL6moJCw8FSoaABiaGj4AMKkFDG0zq7sAHJplDQBCsqY3HxCEglJ7t6YvnCIsUR4KYwARLVtbFA5nCGcRqaCdgA2ONS0VfhZOCTkBUiZLjjcnhK1AIR+UDPSHdhdphIA9McAqtQWAOZQAAa7t7pk+HnzGUVBpQIIvjl62w0YugprtcqtdBJMBBgLJMAgiiMRhsjqIQoI0UgIKNvNA7GgENRgFBrIkAAzbKY1bC+UksfqmY4AKmG+MJxMSAEYAEz+SleakCclqKAcqDCrliqAAZklABZJQBWSUANklAHZJQAOSUATklHKFItFwu5BmYpn6gkZxxZBKJJLQ3N47LQpN0ADoyPwOXpPfwub6vVLA-xZSGFSHlSG1SHNSGdSGDYmfX7TQzmQBRMiQOyUJC+QSCADUUAAymZ5GYxpgoMA0C7uYIAPIAJWLHHA0AshPowDMLjM+KgZlo5AcEzMADcICMQFBMRAwHRkFAwGgJ4pZ8OENYLGZKO7gNR-QrNQnCzBc7IcCMC3i7Q2pbzEvg1qJav56ExzeiBtbbWyDocjKfBAVyqbBsYQA)

Using `this`, the limit seems to be `9`.

[Playground](http://www.typescriptlang.org/play/#code/C4TwDgpgBAgmkDsAmAVA9jATpghiAPFpilBAB7ATIDOUOCIA2gLoA0UKAfFALxQAUAKCgiBw0RKIpGCAK4BbAEYRMzAD7iJIlJqgBKFoL0BuQQEsElTADMcAY2gARHMBxQA3rpzYoALihySiqGAL6moJCw8FSoaABiaGj4AMKkFDG0zq7sAHJplDQBCsqY3HxCEglJ7t6YvnCIsUR4KYwARLVtbFA5nCGcRqaCdgA2ONS0VfhZOCTkBUiZLjjcnhK1AIR+UDPSHdhdphIA9McAqtQWAOZQAAbAABZm1Le6ZPh58xlFQaUCCL4cnptg0Yugpo9nrlVroJJgIMBZJgEEURiMNkdRCFBNikBBRt5oHY0AhqMAoNZEgAGbZTGrYXxUlj9UzHABUwxJZIpiQAnP46V4GQIaWooABGKBigBMUqgAGY5QAWOUAVjlADY5QB2OUADjlvIMzFM-UEbOOnNJ5MpaF5vB5aCpugAdGR+OK9G7+NKve75X7+ErA6rAxrA9rA3rA0bWRyAKJkSB2ShIXyCQQAaigAGUzPIzGNMFBgGhHbzBAB5ABKWY44GgFjJ9GAZhcZhJUGeaQcEzMADcICMQFA8RAwHRkFAwGg+4ph12ENYLGZKC7gNQfaq9UaMzAU7IcCN08TrY7xVSBYl8GtRLV-PQmCacQMLVbubaLw7bbzvReTEAA)

**Related Issues:** 

I have no clue how to search for this.

-----

I came across this weird bug by accident, actually.

I was working on a personal project and this would be the 4th, or 6th time I've rewritten the project. I've noticed that in all the rewrites, there's this particular method on generic class where the max number of times I could chain it was always 20.

The 21st call would always trigger the max depth error.

I was sick of it and decided to investigate possible ways to work around this limit.

I decided to write a simple repro before messing with it. (The above code snippets).
However, the simplified repro behaved **very** weirdly, and would not trigger the error.

It boggles me how TS can resolve crazy types like [this](http://www.typescriptlang.org/play/#code/C4TwDgpgBAgmkDsAmAeGAndAVKEAewEyAzlAIYIgDaAugDRRYB8UAvFABQBQUvnPfQRmxUEAVwC2AIwjoaAHwGDeWJVACUtLuoDcXLgGMANmWKkAGmkw58hEuUq0WAbzXAAhFABcsa3sF4nOreUJZwiKjCWAxRTC5qgugQwGLoCFAIEADuoRy6agC+XEVIEMZkSVAGAPYIxMBQgT6WTno1dQ3obI0AdHh5fQP96oMjw6MT41NDM2Ozk-PTc+pcC8trG0ubizvrw6tbh7vbe8dHpxcnV+fXZ3eXN4+7B-e3D69Pyy-vP29rXEA),

![image](https://user-images.githubusercontent.com/5655961/62441843-bb8f9b80-b723-11e9-9a06-5b249862f697.png)

but will choke on simple types like the above snippets.

-----

It's also super weird because my super complex examples have a limit of 20 calls. And these super simple examples have a super low limit.