Skip to content

[WIP] Report TypeScript bug with aliased Partial duplication - #65562

Draft
Ryan Cavanaugh (RyanCavanaugh) wants to merge 1 commit into
mainfrom
copilot/report-typescript-bug
Draft

Ryan Cavanaugh (RyanCavanaugh) wants to merge 1 commit into
mainfrom
copilot/report-typescript-bug

Conversation

@RyanCavanaugh

Copy link
Copy Markdown
Member

Thanks for asking me to work on this. I will get started on it and keep this PR's description up to date as I form a plan and make progress.


This section details on the original issue you should resolve

<issue_title>Aliased Partial<{ … }> is duplicated when instantiated again: quadratic inference, spurious TS2859, repeated union members</issue_title>
<issue_description>## TypeScript Version

The tested TypeScript version is 7.1.0-dev (main at e086ad3). 7.0.2, 6.0.3, 5.9.3, 5.4.5, 4.9.5 and 4.2.4 behave the same.

OS: macOS 26.5 (arm64). Timings below are from main, with --singleThreaded.

User Goal

The user's project type-checked slowly and they asked me to find out why. It came down to one type alias, in a way that looks like a TypeScript bug, and they asked me to report it.

Setup and Action

slow.ts:

type Digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
type Id = `${0 | 1 | 2 | 3}${Digit}${Digit}${Digit}` // 4,000 ids

type Options = Partial<{ verbose: boolean }>

type State = { [K in Id]?: { id: K; value: number } } & { options?: Options }

declare const state: State
export const keys = Object.entries(state).map((entry) => entry[0])

Run tsc --strict --noEmit --target es2022 --lib es2022 --singleThreaded --extendedDiagnostics slow.ts.

Expected and Actual Behavior

What actually happened: the check takes 5.5 s and 1.1 GB, and reports 2,035 errors like this one:

slow.ts(9,21): error TS2859: Excessive complexity comparing types 'Partial<{ verbose: boolean; }>' and '{ id: "3937"; value: number; }'.

The time is quadratic in the number of ids:

ids Options = Partial<{ verbose: boolean }> Options = { verbose?: boolean }
1,000 0.24 s, 62 MB 0.01 s, 7 MB
2,000 1.2 s, 226 MB 0.01 s, 8 MB
4,000 5.5 s, 888 MB, 2,035 errors 0.01 s, 11 MB

What I expected: the result of the right-hand column. Both declarations of Options describe the same type, and so does Partial<Named> with type Named = { verbose: boolean }, which is also fast.

The cause shows without any timing. After a map that returns its argument, Options is a second type:

declare function either<A, B>(a: A, b: B): A | B

type Options = Partial<{ verbose: boolean }>
declare const values: (Options | string)[]

const both = either(values[0], values.map((value) => value)[0])
//    ^? string | Partial<{ verbose: boolean; }> | Partial<{ verbose: boolean; }>

I expected string | Partial<{ verbose: boolean; }>.

Other ways to get the same result:

  • Readonly<{ … }>, or any alias of { [K in keyof T]: … } applied to a type literal.
  • No alias, with a union of type literals as the argument: Partial<{ a: boolean } | { b: string }>.
  • Two aliases of one instantiation: with type A1 = Partial<Named> and type A2 = Partial<Named>, the type A1 | A2 prints as Partial<Named> | Partial<Named>.

A CPU profile of slow.ts has 82 % of the samples under inferFromMatchingTypes → isTypeIdenticalTo.

In the project where this was found (about 4,000 files, 4 checkers), avoiding the duplicate takes the check time from 15–18 s to 4.7–4.9 s.

Agent Info

Claude Code 2.1.327 + Claude Fable 5.2 + Skill Version 1.0
</issue_description>

Comments on the Issue (you are Copilot in this section)

This branch has not been deployed

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

Labels

Author: Team For Milestone Bug PRs that fix a bug with a specific milestone

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Aliased Partial<{ … }> is duplicated when instantiated again: quadratic inference, spurious TS2859, repeated union members

2 participants