Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error on complex array literals #28707

Closed
wants to merge 4 commits into from
Closed

Conversation

ahejlsberg
Copy link
Member

Large array literals containing object literals that all have distinct types (e.g. because each has a unique property name) end up producing union types with exponentially increasing complexity, due to subtype reduction and object literal normalization (#19513). Rather than "hanging" the type checker by consuming inordinate amounts of memory and time, we now issue an error when an array literal is deemed too complex. The metric we use is 500,000 or more failed subtype checks, which for example would be caused by an array literal containing 1000 or more object literals with distinct types.

const a = [
  { text: "zero", value: 0, "zero": 0 },
  { text: "one", value: 1, "one": 1 },
  { text: "two", value: 2, "two": 2 },
  // ... thousands more
];

The "Array literal type is too complex to represent" error we now report is easily squelched using a type assertion on the first element:

const a = [
  { text: "zero", value: 0, "zero": 0 } as any,
  { text: "one", value: 1, "one": 1 },
  { text: "two", value: 2, "two": 2 },
  // ... thousands more
];

This causes the array literal to have the inferred type any[].

Fixes #28540.

Copy link
Member

@RyanCavanaugh RyanCavanaugh left a comment

Choose a reason for hiding this comment

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

Needs merge conflict resolution

@weswigham
Copy link
Member

@RyanCavanaugh probably worth mentioning that #28727 fixes the same original bug with a smaller fallout of disabling-things-which-really-should-work. (it's not linked in this PR, but is mentioned in the bug this PR fixes)

@ahejlsberg
Copy link
Member Author

Closing in favor of #29435.

@ahejlsberg ahejlsberg closed this Jan 28, 2019
@ahejlsberg ahejlsberg deleted the errorComplexArrayLiteral branch February 12, 2019 15:31
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.

3 participants