Skip to content

Fix integer overflow in getCrossProductUnionSize causing hang on large template literal types#3929

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-tsgo-hang-large-template-types
Draft

Fix integer overflow in getCrossProductUnionSize causing hang on large template literal types#3929
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-tsgo-hang-large-template-types

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 16, 2026

getCrossProductUnionSize overflows Go's signed 64-bit int when computing large cross products (e.g., 4^49 for 49 unions of 4 members). At 4^32 = 2^64, the product wraps to 0, bypassing the 100,000 limit check entirely. In TypeScript's reference implementation, JS floats produce Infinity which correctly triggers the error.

  • Fix: Early-exit in getCrossProductUnionSize when the next multiplication would exceed the 100k threshold, preventing overflow and correctly returning the cap value
case t.flags&TypeFlagsUnion != 0:
	n := len(t.Types())
	if n > 0 && size > 100_000/n {
		return 100_000
	}
	size *= n
  • Test: Added templateLiteralTypeTooComplex.ts covering the exact repro from the issue — confirms TS2590 is emitted instead of hanging

…e template literal types

Add overflow protection to prevent int64 wraparound when computing cross
product union sizes. When the size would exceed 100,000, return 100,000
immediately instead of continuing to multiply, which could overflow and
produce a value that bypasses the limit check.

Add test case for template literal type with combinatorial explosion.

Agent-Logs-Url: https://github.com/microsoft/typescript-go/sessions/5fcc3e3e-691e-43a4-b71c-72ae84389f8b

Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix tsgo hang on large template literal types Fix integer overflow in getCrossProductUnionSize causing hang on large template literal types May 16, 2026
Copilot AI requested a review from jakebailey May 16, 2026 20:29
case t.flags&TypeFlagsUnion != 0:
size *= len(t.Types())
n := len(t.Types())
if n > 0 && size > 100_000/n {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot 100_000 seems like a magic number, and this definitely requires a comment. Probably also best to return MaxInt?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tsgo hangs on large template literal types with combinatorial explosion

2 participants