Repository: TypeScript
Issue: `RangeError: Maximum call stack size exceeded` Regression in #52392
Labels: Bug

### 🔎 Search Terms

RangeError: Maximum call stack size exceeded, #52392

### 🕗 Version & Regression Information

- This changed in commit or PR #52392


### ⏯ Playground Link

https://www.typescriptlang.org/play/?#code/KYDwDg9gTgLgBAbwL4G4BQaAmwDGAbAQymDgICMBnGKAnefAiiuAFQE8xgARAmAgWQjY8AHgAKRYADsYAPkRo4cMJJkAuOBOIz0i5cRXEuwKAEsAbsEw8+ACj1KYAC1MUNz13FAxpmZgmVVGA1TKQAzEzgAfTgkOAB+OClgSyg4DWTUvQBKDXMIU0x0JAxsBmI4BiY4ACFGYABJHwBbLxAfKT84LggcAFdm6RgRAik2eQQ9QyGNOopGloA6LSHirFxCCqkCQYoVHBI5heBWyccOEhWZOABeOBgLiDC4AEE6aDEWACUAJmA19blEgAczwEDIBDwCiU212+xIYQgfU6UDY0KUbUgsDgpmaWPgUQAqlI+vNMLdavUmid0EoSiVAZsSFVmNTWt5fMwev1BtdkKUNpJKoRqgAxCB4bBQT6-EhnOAgdyPZ5vGAfb5-AFlJnCxjMVXq2XothKzhPODiyUmGWavQALw07E4NgEQmAogeZuebNkAM9JGMwDAWhgpkhIhYbQ6XXBACtcHIKfKANoAaRxUjgAGtgGxzSwALrxDRRQPBoih8MsNMF31oVBof3RMshsOiFjyO6RjmdZhxhMJbrAIOtqvyR26bVClndXoDIYAZScw5gADUfiJufOZAAaOAAYQg4VMwL6NFDR6jnLg-boFNG43ROCPVCgfXeUHsGJvYAvUjcQ4jhWbYiIex6nuephHvIABk6Lfpgc68sEs48qsDixDkWqChUz7-vAiJWlAprAOalpSja-xoMkADuqHbjAS4ruutgBIhaHqHAhFSrE2ToEAA

### 💻 Code

```ts
export {};

declare abstract class TypeDataModel<Parent> {
  parent: Parent;

  prepareDerivedData(
    this: this extends { parent: infer _ } ? never : never
  ): void;
}

declare class BaseItem extends Document<any> {
  parent: BaseItem.Parent;
}
declare namespace BaseItem {
  type Parent = typeof ActorPTR2e;
}

declare global {
  namespace foundry {
    export import _Unused = BaseItem;
  }
}

declare class Item extends Document {}

declare class FolderPTR2e {
  x: typeof ActorPTR2e;
}

declare class ActorPTR2e {
  y: typeof FolderPTR2e;
  z: TypeDataModel<typeof Item>;
}

type DeepPartial<T extends object> = {
  [K in keyof T]?: _DeepPartial<T[K]>;
};
type _DeepPartial<T> = T extends object ? DeepPartial<T> : T;

declare class DocumentSheetV2<Document, Configuration extends object = any> {
  constructor(
    options: DeepPartial<Configuration> & {
      document: Document;
    }
  );
}

declare const folder: typeof FolderPTR2e;
new DocumentSheetV2({ document: folder });
```


### 🙁 Actual behavior

The compiler crashes with `RangeError: Maximum call stack size exceeded`.

```
RangeError: Maximum call stack size exceeded
    at structuredTypeRelatedToWorker (node_modules/typescript/lib/_tsc.js:64885:43)
    at structuredTypeRelatedTo (node_modules/typescript/lib/_tsc.js:64817:21)
    at recursiveTypeRelatedTo (node_modules/typescript/lib/_tsc.js:64768:19)
    at isRelatedTo (node_modules/typescript/lib/_tsc.js:64186:122)
    at isPropertySymbolTypeRelated (node_modules/typescript/lib/_tsc.js:65563:14)
    at propertyRelatedTo (node_modules/typescript/lib/_tsc.js:65603:23)
    at propertiesRelatedTo (node_modules/typescript/lib/_tsc.js:65814:29)
    at structuredTypeRelatedToWorker (node_modules/typescript/lib/_tsc.js:65361:21)
    at structuredTypeRelatedTo (node_modules/typescript/lib/_tsc.js:64817:21)
    at recursiveTypeRelatedTo (node_modules/typescript/lib/_tsc.js:64768:19)
```
(this is on 5.8.0-dev.20250106 but the same error appears on 5.7.2 and several other versions I tested as well as all the ones I bisected)

### 🙂 Expected behavior

Regular errors. This file has a few but in my original repo there were less, possibly none?

The editor doesn't crash and reports these errors:
```
Type instantiation is excessively deep and possibly infinite.
Expression produces a union type that is too complex to represent.
```
On `DocumentSheetV2`. I find the second one odd because there's not a single union in the program.

There's also the error:
```
No base constructor has the specified number of type arguments.
```

On `Document<any>`, because `Document` doesn't take type arguments. In my original repo I wasn't passing a generic to a class without one but this was the easiest way to reliably reproduce it. I can get it to work _without_ the `<any>` but it was simply less reliable while I was reducing so I left it in.

### Additional information about the issue

_No response_