Description
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:
projectA/withSomething.d.ts
is very large, as seen on the playground, see https://github.com/ms/ts-hoc-repro/blob/master/projectA/withSomething.d.tsprojectB/
won't type check because it complains about a missing prop, but that prop is optional in the inner component (so the HOC return type is incorrect)
🙁 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)
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.)