TS 3.9.3 breaks HOC around generic class-based components that worked with TS 3.8.3 #38910
Labels
Design Limitation
Constraints of the existing architecture prevent this from being fixed
Rescheduled
This issue was previously scheduled to an earlier milestone
Milestone
Uh oh!
There was an error while loading. Please reload this page.
TypeScript Version: 3.9.3
Search Terms:
Code
tsconfig.json
package.json
Additional explanations
The error can be reproduced by executingnpm run compile
. The example is silly, of course, but it's the most compact way to demonstrate the problem. I want to be able to decorate a class-based component with a generic type parameter. (I want to use my custom library component in different places with different type instantiations, thus removing<P>
fromCustomLibraryComponent
solves the problem described here but my actual use case requires the generic parameter, so please ignore thatP
is not actually used in the example above.) And I'm not interested in the assignment per se but the following, more realistic code fails due to the same reason: In my actual use case, the decorator itself is also generic but this just adds unnecessary detail and complexity here.Expected behavior: Either TS 3.9.3 should still not complain or give me a way to fix the issue.
Actual behavior: Compiling the above code results in
It's not clear to me whether this is really a problem with TypeScript, actually. The reason why I decided to report this issue here (rather than at DefinitelyTyped or simply asking on StackOverflow) is because it isn't obvious to me why TypeScript complains about
Type 'Readonly<any> & Readonly<{ children?: ReactNode; }>' is not assignable to type 'Readonly<CustomProperties> & Readonly<{ children?: ReactNode; }>'.
Additionally, it's clearly TypeScript that introduced the error. Using"typescript": "3.8.3"
, the above code works fine.An alternative to permitting this assignment again would be to somehow tell TypeScript the type to use instead of
any
but I have no idea how. The following alternatives all fail with different errors:If the error could be fixed by improving the typing of React, please let me know (ideally with a suggestion how to fix it there).
Playground Link
Related Issues: I assume the error has been introduced with the pull request #37195. The only other regression I found in this regard is #38542.
Workaround: Interestingly, generic functional components still work. The above code compiles with TypeScript 3.9.3 when the
CustomLibraryComponent
is declared as follows:The text was updated successfully, but these errors were encountered: