Skip to content

React HOCs generate extremely large and incorrect .d.ts declaration files, regression from 4.1.3 #43622

Closed
@ms

Description

@ms

Bug Report

While trying to upgrade from 4.1.3 to 4.2.3 in our monorepo, we ran into a regression that's preventing us from upgrading at the moment. (This is the same monorepo mentioned in #41570 cc @brieb @lencioni)

When using React HOCs and letting TS infer returned types, the type inferred and included in the corresponding .d.ts is:

  • much larger, with a large number of conditional types
  • incorrect, in some instances, removing wrapped component props optionality
  • inconsistent with direct type checking (leading to the language server reporting no errors but tsc reporting errors, in some cases)
  • much slower during compilation

🔎 Search Terms

HOC

Declaration mismatch

Upgrade

Performance regression

🕗 Version & Regression Information

4.2.* seems to exhibit the issue. A git bisect with locally built compilers seems to point to 34f0e32 as the first bad commit (however, despite the title of the commit, the npm package for 4.2.1 also has the issue).

⏯ Playground Link

Playground link with relevant code

Note that your browser may slow down significantly. Check the .d.ts tab. Compare with version 4.1.3.

This does not fully reproduce the error because of cross project dependencies (the type is not only large but also incorrect).

💻 Code

To reproduce the issue in more details, check https://github.com/ms/ts-hoc-repro (to reproduce: npm i && npx tsc -p projectA/ && npx tsc -p projectB/). The two issues:

🙁 Actual behavior

  • Incorrect type for wrapped components
  • Large .d.ts
  • Slow compilation using composite: true because of the large .d.ts

🙂 Expected behavior

  • Correct type for wrapped component
  • .d.ts files equivalent to 4.1.3
  • No speed regression on compilation time

Last, I ran tsc with --generateTrace for 4.1 and 4.2. Here's a screenshot for 4.2 with transformNodes, in the emit phase, taking multiple seconds (this call is invisible for 4.1)

image

Workaround

We've been able to mitigate this issue by explicitly annotating the return type of the HOC (using the exact same generic helpers, ElementConfig, etc.)

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: Declaration EmitThe issue relates to the emission of d.ts filesDomain: PerformanceReports of unusually slow behaviorFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions